ผมเขียนไฟล์ Server ได้เเล้วครับเเต่ไม่สามารถโหลดไฟล์ได้
โดยผมกําหนดให้ Client โหลดไฟล์จาก Server จากพาท C:/ftpService/
//ไฟล์ฝั่ง Server
using System;
using System.Net;
using System.IO;
using System.Net.Sockets;
class FtpServer
{
public static void Main()
{
/* Creating Port no 3099 on server for listening the client request*/
TcpListener listener = new TcpListener(3099);
Console.WriteLine(" Welcome to FTP Server.");
/*Show Computer Name and IP Address*/
string hostName = Dns.GetHostName();
Console.WriteLine("\n Local hostname: {0}",hostName);
IPHostEntry myself = Dns.GetHostByName(hostName);
foreach (IPAddress address in myself.AddressList)
Console.WriteLine("\n IP Address: {0}", address.ToString());
Console.WriteLine("\n Server Starts and Listening on Port(3099)....");
/* start listening for any request*/
listener.Start();
/* calling function Service*/
Service(listener);
}
public static void Service(TcpListener server)
{
while (true)
{
//creating object of TcpClient to catch the stream of connected computer
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("\n\nConnected");
//getting the networkclient stream object
NetworkStream clientstream = client.GetStream();
//creating streamreader object to read messages from client
StreamReader reader = new StreamReader(clientstream);
//creating streamwriter object to send messages to client
StreamWriter writer = new StreamWriter(clientstream);
writer.AutoFlush = true;
// reading file name from client
string sourcefile = reader.ReadLine();
Stream inputstream;
try
{
//opening file in read mode
inputstream = File.OpenRead(sourcefile);
//sending file name to the client
writer.WriteLine(sourcefile);
}
catch
{
Console.WriteLine("\n\n File not found named: {0}", sourcefile);
//sending message to client if file not found
writer.WriteLine("\n\nFile not found");
clientstream.Close();
continue;
}
const int sizebuff = 1024;
try
{
/*creating the bufferedstrem object for reading 1024 size of bytes from the
file */
BufferedStream bufferedinput = new BufferedStream(inputstream);
/*creating the bufferedstrem object for sending bytes which are read
from file */
BufferedStream bufferedoutput = new BufferedStream(clientstream);
/* creating array of bytes size is 1024 */
byte[] buffer = new Byte[sizebuff];
int bytesread;
/* Reading bytes from the file until the end */
while ((bytesread = bufferedinput.Read(buffer, 0, sizebuff)) > 0)
{
/* sending the bytes to the client */
bufferedoutput.Write(buffer, 0, bytesread);
}
Console.WriteLine("\n\n file copied name:{0}", sourcefile);
/* Closing connections*/
bufferedoutput.Flush();
bufferedinput.Close();
bufferedoutput.Close();
}
catch (Exception)
{
Console.WriteLine("\n\n Connection Couldnot Esablished because Client forget to create-\n \"ftpService\" folder in his (C) Drive or his/her harddisk is full or Client close its Connection in between process");
writer.Close();
reader.Close();
continue;
}
/* Closing connections*/
writer.Close();
reader.Close();
}
}
}
เมื่อรันโปรเเกรมจะได้รูปตามนี้ครับ
มาถึงฝั่ง Client นะครับ
Code (C#)
//code Client
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net.Sockets;
using System.Net;
using System.Reflection;
namespace Client_Project
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//botton disconnect
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
//botton connect
private void button1_Click(object sender, EventArgs e)
{
//creating object of TcpClient to catch the stream of connected computer
TcpClient Server = null;
try
{
IPAddress myIP = IPAddress.Parse(textBox1.Text);
//sending the computer IPAddress and port number on the network
IPHostEntry entry = Dns.GetHostByAddress(myIP);
Server = new TcpClient(entry.HostName, 3099);
MessageBox.Show("Connected.");
}
catch (Exception)
{
MessageBox.Show("Server Not found Enter Correct Machine Name.");
return;
}
}
private void button4_Click(object sender, EventArgs e)
{
}
OpenFileDialog ofd = new OpenFileDialog();
private void button3_Click(object sender, EventArgs e)
{
if (ofd.ShowDialog() == DialogResult.OK)
{
textBox2.Text = ofd.FileName;
}
}
private void button5_Click(object sender, EventArgs e)
{
}
}
}