TcpClient 在异步读取期间断开连接
我有几个关于完成 tcp 连接的问题。
客户端使用 Tcp 连接到我的服务器,在使用
listener.BeginAcceptTcpClient(ConnectionEstabilishedCallback, null);
接受客户端后,我开始使用networkStream.BeginRead(.... )
。
当我等待消息时客户端断开连接会发生什么? (例如断电、断网等)
我如何知道它何时发生?如果在成功读取后,我做了一些事情,然后调用
networkStream.Close(); client.Close();
客户端会看到什么?如何“优雅”地终止连接?如果我正在等待读取(使用 BeginRead),然后(在不同的线程上)关闭同一个流,会发生什么情况?
编辑添加:我确实在客户端和服务器之间发生了乒乓消息。够了吗?如果我没有收到 ping,是否终止我的 NetworkStream?肯定有更好的东西。
I have a few questions about finishing a tcp connection.
A client connects to my server using Tcp, after accepting the client with
listener.BeginAcceptTcpClient(ConnectionEstabilishedCallback, null);
, I start to read withnetworkStream.BeginRead(....)
.
What happens when the client disconnects while I wait for a message? (e.g. it loses power, internet, etc)
How do I know when it happens?If after a successful read, I do some stuff, and then call
networkStream.Close(); client.Close();
What will the client see? How do I terminate the connection "gracefully"?What happens if I'm waiting for a read (with BeginRead), and then (on a different thread) close the same stream?
EDIT TO ADD: I do have a ping-pong message going on between the client and server. Is that enough? If I don't receive ping, terminate my NetworkStream? surely there must be something better.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
1-如果客户端由于电缆拔出而断开连接,您将不知道直到下一次读取或写入套接字。另请注意,tcpClient.Connected 属性值不可靠,它的值取决于上次通信;因此,如果最后一次通信成功,则其值为 true,否则为 false。有关该内容的详细信息,请查看此 。
2- 如果您关闭网络流和客户端,则客户端会正常终止。
3、我不知道,你测试一下吧。
如果您意识到由于电缆拔出等原因导致连接丢失,那么要获得适当的 IsConnected 值,您必须意识到在读取或写入 tcp 期间连接丢失,因此您需要通过盆栽尝试来访问 tcpclient 成员-catch 围绕其操作...
使用此 IsConnected 属性检查 tcpClient 是否已连接:
1- If the client disconnected due to cable unplugged you will not know till the next read or write to the socket. also note that the tcpClient.Connected property value is not reliable, it's value depending on the last communication; so if the last communication were succeed then it's value is true otherwise it is false. for more information on that check this.
2- If you close the network stream and the client this is the gracefully termination of the client.
3- I don't know, give it a test.
If you aware from connection lost because of a cable unplugged or so, then to get appropriate IsConnected value you have to be aware from the connection lost during the read or write to the tcp, so you need to access the tcpclient members by potting a try-catch around its operation....
Use this IsConnected property to check if the tcpClient is connected:
由于不同的断开原因,会发生不同的情况。
当您从
socket.EndRead
接收到 0 个字节时,就会检测到正常断开连接。其他关闭将导致EndRead
或Send
出现SocketException
关闭会优雅地做到这一点。
你会得到一个例外。
ObjectDisposeException
(如果您处置客户端)或IOException
Different things happens for different disconnect reasons.
A graceful disconnection is detected when you receive 0 bytes from
socket.EndRead
. Other shutdowns will lead toSocketException
for eitherEndRead
orSend
Close will do it gracefully.
You'll get an exception. Either
ObjectDisposedException
(if you dispose the client) orIOException