c# 客户端主动断开网络连接怎么做?
tcpClient只有connect方法没有disconnet方法吗?Close方法不能关闭连接。找到了:tcpClient.Client.Disconnect(fals...
tcpClient 只有connect方法 没有 disconnet方法吗? Close方法不能关闭连接。
找到了: tcpClient.Client.Disconnect(false); 展开
找到了: tcpClient.Client.Disconnect(false); 展开
1个回答
推荐于2016-11-20 · 知道合伙人软件行家
关注
展开全部
TcpClient tcpClient = new TcpClient();
tcpClient.Connect("contosoServer", 11000);
// Get the stream used to read the message sent by the server.
NetworkStream networkStream = tcpClient.GetStream();
// Set a 10 millisecond timeout for reading.
networkStream.ReadTimeout = 10;
// Read the server message into a byte buffer.
byte[] bytes = new byte[1024];
networkStream.Read(bytes, 0, 1024);
//Convert the server's message into a string and display it.
string data = Encoding.UTF8.GetString(bytes);
Console.WriteLine("Server sent message: {0}", data);
networkStream.Close(); ----- 这个才关闭连接,你用的是2.0的。新版本 是自动关闭的。
tcpClient.Close();
tcpClient.Connect("contosoServer", 11000);
// Get the stream used to read the message sent by the server.
NetworkStream networkStream = tcpClient.GetStream();
// Set a 10 millisecond timeout for reading.
networkStream.ReadTimeout = 10;
// Read the server message into a byte buffer.
byte[] bytes = new byte[1024];
networkStream.Read(bytes, 0, 1024);
//Convert the server's message into a string and display it.
string data = Encoding.UTF8.GetString(bytes);
Console.WriteLine("Server sent message: {0}", data);
networkStream.Close(); ----- 这个才关闭连接,你用的是2.0的。新版本 是自动关闭的。
tcpClient.Close();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询