Search code examples
c#socketssslstream

C# Is using Socket.Connected breaks the SslStream?


I want to know whether using Socket.Connected to determine if the socket still connected breaks the SslStream or not.


Solution

  • No, it shouldn't, because it does not actually do anything to the socket. Are you trying to figure out if the server at the other end is still there? If so, you may want to set up some type of ping, because this property will not tell you if the connection was interrupted unexpectedly (e.g. program crashing, network going down, etc.).

    From MSDN:

    The Connected property gets the connection state of the Socket as of the last I/O operation. When it returns false, the Socket was either never connected, or is no longer connected.

    The value of the Connected property reflects the state of the connection as of the most recent operation. If you need to determine the current state of the connection, make a nonblocking, zero-byte Send call. If the call returns successfully or throws a WAEWOULDBLOCK error code (10035), then the socket is still connected; otherwise, the socket is no longer connected.

    If you call Connect on a User Datagram Protocol (UDP) socket, the Connected property always returns true; however, this action does not change the inherent connectionless nature of UDP.