Client端主要代码: /////////////////////////////////////////////////////////////////////////////////
//连接服务器
////////////////////////////////////////////////////////////////////////////////
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Connect(EPhost); //EPhost,服务器IP与端口
while(true)
{
Console.WriteLine("请选择一种操作:1.上传文件 2.下载文件 3.退出");
int choice;
choice = int.Parse(Console.ReadLine());
Console.WriteLine(""); switch (choice)
{
case 1:
{
//发送服务请求
Get = "上传";
ByteGet = Encoding.Unicode.GetBytes(Get); //Byte[] ByteGet; s.Send(ByteGet, ByteGet.Length, SocketFlags.None);
this.Updata();
break;
} ………………………………
}
} /////////////////////////////////////////////////////////////////////////////////
//上传文件,位于void UpData()方法里
////////////////////////////////////////////////////////////////////////////////
if (canSend.Equals("Begin"))
{
byte[] sendBuffer = new byte[2048]; while ((i = fs.Read(sendBuffer, 0, 2048)) != 0) //FileStream fs,用于读取文件
{
fs.Flush(); ns.Write(sendBuffer, 0, i); //NetworkStream ns,用于传输文件数据流的网络基础数据流
ns.Flush();
}
Console.WriteLine("\n上传成功!\n\n");
} fs.Close();
ns.Close();
s.Disconnect(true);
server端主要代码: //////////////////////////////////////////////////////////////////////////////////////////
//接受文件数据
/////////////////////////////////////////////////////////////////////////////////////////
Byte[] fileByte = new Byte[2048]; int i; while ((i = ns.Read(fileByte, 0, 2048)) != 0)
{
ns.Flush(); fs.Write(fileByte, 0, i);
fs.Flush();
} Console.WriteLine("\n接受客户端上传成功!\n\n");
fs.Close();
ns.Close();经过运行,成功的把文件上传至服务器,但是有个问题一直解决不了。服务器接受文件采用的方法是ns.Read(fileByte, 0, 2048),所以当客户端传完文件,如果不关闭socket,那么服务器端回一直停在ns.Read(fileByte, 0, 2048)那里,等待客户端传数据而不进行下面的代码。为了解决这个问题,我就在客户端传完数据后关闭客户端socket,为了重复利用socket,我用了这样的关闭方法s.Disconnect(true); 但是执行这个方法后,不知怎么回事,会一直停留在这个语句中,不进行下面的操作,也就是跳出上传文件UpData()方法,回到主菜单中。后来我用s.Close()方法关闭了socket,这样是解决了问题,但是在每次调用上传文件或者下载文件方法的时候,都要new一个socket,然后再重新连接。但是总觉得这样性能很不好。不知道有没有什么办法不需要完全关闭socket,达到socket重用。
//连接服务器
////////////////////////////////////////////////////////////////////////////////
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Connect(EPhost); //EPhost,服务器IP与端口
while(true)
{
Console.WriteLine("请选择一种操作:1.上传文件 2.下载文件 3.退出");
int choice;
choice = int.Parse(Console.ReadLine());
Console.WriteLine(""); switch (choice)
{
case 1:
{
//发送服务请求
Get = "上传";
ByteGet = Encoding.Unicode.GetBytes(Get); //Byte[] ByteGet; s.Send(ByteGet, ByteGet.Length, SocketFlags.None);
this.Updata();
break;
} ………………………………
}
} /////////////////////////////////////////////////////////////////////////////////
//上传文件,位于void UpData()方法里
////////////////////////////////////////////////////////////////////////////////
if (canSend.Equals("Begin"))
{
byte[] sendBuffer = new byte[2048]; while ((i = fs.Read(sendBuffer, 0, 2048)) != 0) //FileStream fs,用于读取文件
{
fs.Flush(); ns.Write(sendBuffer, 0, i); //NetworkStream ns,用于传输文件数据流的网络基础数据流
ns.Flush();
}
Console.WriteLine("\n上传成功!\n\n");
} fs.Close();
ns.Close();
s.Disconnect(true);
server端主要代码: //////////////////////////////////////////////////////////////////////////////////////////
//接受文件数据
/////////////////////////////////////////////////////////////////////////////////////////
Byte[] fileByte = new Byte[2048]; int i; while ((i = ns.Read(fileByte, 0, 2048)) != 0)
{
ns.Flush(); fs.Write(fileByte, 0, i);
fs.Flush();
} Console.WriteLine("\n接受客户端上传成功!\n\n");
fs.Close();
ns.Close();经过运行,成功的把文件上传至服务器,但是有个问题一直解决不了。服务器接受文件采用的方法是ns.Read(fileByte, 0, 2048),所以当客户端传完文件,如果不关闭socket,那么服务器端回一直停在ns.Read(fileByte, 0, 2048)那里,等待客户端传数据而不进行下面的代码。为了解决这个问题,我就在客户端传完数据后关闭客户端socket,为了重复利用socket,我用了这样的关闭方法s.Disconnect(true); 但是执行这个方法后,不知怎么回事,会一直停留在这个语句中,不进行下面的操作,也就是跳出上传文件UpData()方法,回到主菜单中。后来我用s.Close()方法关闭了socket,这样是解决了问题,但是在每次调用上传文件或者下载文件方法的时候,都要new一个socket,然后再重新连接。但是总觉得这样性能很不好。不知道有没有什么办法不需要完全关闭socket,达到socket重用。
刚搜索到得,我也在看。呵呵
SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);