c# 客户端调用Socket之后,服务端出现大量close wait状态的tcp连接,请问客户端如何正确关闭socket 20
客户端代码如下IPAddressip=IPAddress.Parse("127.0.0.1");IPEndPointipe=newIPEndPoint(ip,8885);...
客户端代码如下
IPAddress ip = IPAddress.Parse("127.0.0.1");
IPEndPoint ipe = new IPEndPoint(ip, 8885);//把ip和端口转化为IPEndPoint实例
Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
c.Connect(ipe);//连接到服务器
byte[] bs = Encoding.Default.GetBytes("001");
Console.WriteLine("Send Message");
c.Send(bs, bs.Length, 0);//发送测试信息
string recvStr = "";
byte[] recvBytes = new byte[4096];
var length = c.Receive(recvBytes, recvBytes.Length, 0);//从服务器端接受返回信息
recvStr += Encoding.Default.GetString(recvBytes, 0, length);
c.Shutdown(SocketShutdown.Both);
c.Close(); 展开
IPAddress ip = IPAddress.Parse("127.0.0.1");
IPEndPoint ipe = new IPEndPoint(ip, 8885);//把ip和端口转化为IPEndPoint实例
Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
c.Connect(ipe);//连接到服务器
byte[] bs = Encoding.Default.GetBytes("001");
Console.WriteLine("Send Message");
c.Send(bs, bs.Length, 0);//发送测试信息
string recvStr = "";
byte[] recvBytes = new byte[4096];
var length = c.Receive(recvBytes, recvBytes.Length, 0);//从服务器端接受返回信息
recvStr += Encoding.Default.GetString(recvBytes, 0, length);
c.Shutdown(SocketShutdown.Both);
c.Close(); 展开
2017-03-07
展开全部
你先shutdown了,再调用close就收到不服务器的反馈了,所以只能是WAIT了,最后两行,你只用其中的一行就行了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询