简体   繁体   English

当我的一个客户端崩溃时,NetworkStream.Write挂起我的服务器应用程序

[英]NetworkStream.Write hangs my sever app when one of my clients crashed

I have a clien-server .net app, those communicate via tcp. 我有一个clien-server .net应用程序,通过tcp进行通信。 My server sends raw byte packets periodically to my clients. 我的服务器定期向客户端发送原始字节数据包。

It works great 99.9% of the time, but sometimes, when one of my clients crashes (twice in a year, not more) my server hangs. 它有99.9%的时间很好用,但有时候,当我的一个客户崩溃时(一年两次,而不是更多)我的服务器挂起。

I debugged it (by adding some test code to my client that crashes my app), and noticed to problem is in stream.Write(bytes, 0, bytes.Length); 我调试它(​​通过向我的客户端添加一些测试代码来崩溃我的应用程序),并注意到问题在于stream.Write(bytes, 0, bytes.Length); . It simple does not do anything, no respond, no return, just hangs. 它简单没有做任何事情,没有回应,没有回报,只是挂起。 Stream is a NetworkStream. Stream是一个NetworkStream。

I check stream.CanWrite before, but it responds true. 我之前检查了stream.CanWrite ,但它的响应是真的。

So, how can I check in server side, that one of my clients crashed? 那么,我如何检查服务器端,我的一个客户崩溃了? What should I check on NetworkStream (or something else) before sending data to other clients? 在将数据发送到其他客户端之前,我应该检查NetworkStream(或其他什么)?

Set timeout to the socket of your TCP connection. 将超时设置为TCP连接的套接字。 Looks like now it is infinite, so server waits for response from dead client indefinitely. 看起来现在它是无限的,所以服务器无限期地等待来自死客户端的响应。

Write may hang simply because the other side is not reading from the socket and their OS socket buffer is full (but they're not closing the socket nor is it in an erroneous state). 写入可能只是因为另一方没有从套接字读取并且它们的OS套接字缓冲区已满(但它们没有关闭套接字,也没有处于错误状态)。

Here's a description of such scenario: http://stringarray.net/?p=29 以下是对此类场景的描述: http//stringarray.net/? p = 29

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM