4 Comments

  1. Bob

    Thank you for this Post. It sounds promising for me but there is one thing I don’t understand.
    After configuring the timeout, how to I recognize that a timeout occurred?
    I tried your code with the socket of a TcpClient, where I do read/write operations using the Networkstream of the object. But I don’t get a signal or exception when I Shutdown the server. So how can I check if die Connection was timed out?

    • Hey Bob,
      I believe you should be seeing an IOException eventually. it wouldn’t happen right away – it depends on what value you have set for your read/write timeouts. Setting the keepalive will send a little ping message every so often to make sure the connection is still alive, so it still uses your write timeout when determining if the connection has failed or not. Hope this helps!

  2. Oliver Schramm

    Hey,

    client.Client.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, 2000);

    should work cross-platform and send a KeepAlive packet every 2 seconds. There’s also SocketOptionName.TcpKeepAliveInterval which is the time the socket waits for an answer before another KeepAlive packet is sent.

Leave a Reply

Your email address will not be published. Required fields are marked *