简体   繁体   中英

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

I have a clien-server .net app, those communicate via 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.

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); . It simple does not do anything, no respond, no return, just hangs. Stream is a NetworkStream.

I check stream.CanWrite before, but it responds true.

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?

Set timeout to the socket of your TCP connection. 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).

Here's a description of such scenario: http://stringarray.net/?p=29

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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