App hangs when a client crashes

Mar 27, 2013 at 1:53 PM
First of all, thanks for this great library!

I a testing it for a while, and found a problem. I have a client-server app, where server sends raw bytes to the clients by tcp periodically. When one of my clients crashes (it can happen sometimes on Windows :D ), the whole communication, the whole app hangs. I use Server.DispatchAll to send data to the clients.

I debugged, and found, the problem is in your NetBaseStream class:
protected void SendRaw(byte[] data)
        {
            byte[] bytes = data;
            if (IsActive && stream.CanWrite)
            {
                try
                {
                    stream.Write(bytes, 0, bytes.Length);
                    DataSent += bytes.LongLength;
                }
                catch (SocketException ex)
                {
                    Stop(NetStoppedReason.Remote);
                    return;
                }
            }
        }
stream.Write waits infinitely, so when I have a corrupt connection by a client crash, everything hangs. The solution is to set NetworkStream's WriteTimeout property to a non-infinite value.

As I see these all classes are private/protected in your code, so there is no way to set it, the only way is by changing your code. So, it would be nice to enable to set it from a public property/method.

Thanks!