TcpClient.Close 不会关闭连接 [英] TcpClient.Close doesn't close the connection

查看:88
本文介绍了TcpClient.Close 不会关闭连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它使用 TcpClient 和 TcpListener 通过网络进行通信.但是,当我在客户端上调用 TcpClient.Close 以断开它与服务器的连接时,服务器根本没有反应.

I have an application that uses TcpClient and TcpListener to communicate over the network. However, when I call TcpClient.Close on the client to disconnect it from the server, the server doesn't react at all.

现在,在您发表关于此问题的评论是 这个,以及如何在这里找到解决方案,相信我,当我说我已经找到这些并尝试过它们时.它没有帮助.我还尝试了 TcpClient.CloseTcpClient.GetStream().Close()TcpClient.Dispose 的不同组合.没有任何效果.

Now, before you post a comment about this question being a duplicate of this one, and how the solution can be found here, believe me when I say that I've already found these and tried them. It doesn't help. I've also tried different combinations of TcpClient.Close, TcpClient.GetStream().Close(), and TcpClient.Dispose. Nothing works.

代码没有什么值得注意的,只是客户端中的一个 Disconnect 方法,它重置所有变量以供重用,并关闭所有网络资源.服务器有一个循环来检查 TcpClient.Connected 是否为真,如果为假,它应该跳出循环并终止线程.

The code is nothing noteworthy, just a Disconnect method in the client that resets all of the variables for reuse, and closes all network resources. The server has a loop that checks if TcpClient.Connected is true or not, and if it's false, it's supposed to jump out of the loop and terminate the thread.

有什么想法吗?

推荐答案

TcpClient.Connected 几乎应该被忽略.它基本上代表了上次通信是否成功.来自 MSDN(重点是我的):

The TcpClient.Connected should pretty much be ignored. It basically represents whether the last communication was successful. From MSDN (emphasis mine):

因为 Connected 属性只反映最近操作时的连接状态,所以您应该尝试发送或接收消息以确定当前状态.消息发送失败后,此属性不再返回 true.请注意,此行为是设计使然.您无法可靠地测试连接状态,因为在测试和发送/接收之间的时间内,连接可能已丢失.您的代码应该假设套接字已连接,并优雅地处理失败的传输.

Because the Connected property only reflects the state of the connection as of the most recent operation, you should attempt to send or receive a message to determine the current state. After the message send fails, this property no longer returns true. Note that this behavior is by design. You cannot reliably test the state of the connection because, in the time between the test and a send/receive, the connection could have been lost. Your code should assume the socket is connected, and gracefully handle failed transmissions.

如果您在客户端调用 Close(),则不会向服务器发送任何内容来告诉它关闭,它实际上只是自行关闭,因此客户端无法使用它更多的.确定您是否仍处于连接状态的唯一可靠方法是尝试发送数据并处理故障.如果你愿意,你可以实现你自己的握手协议,当你调用 Close() 时,你会向服务器发送一个特殊的通知,提醒它注意这一事实,但有时该数据包从未到达服务器.

If you call Close() on the client side, nothing is sent to the server to tell it that its closing, it literally just closes it self so that the client can't use it any more. The only reliable way to determine if you're still connected is to try to send data and handle the failure. If you want you could implement your own handshake agreement where when you call Close() you send a special notification to the server alerting it to the fact but there will still be times when that packet never reaches the server.

这篇关于TcpClient.Close 不会关闭连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆