Daniel Eugen
Daniel Eugen

Reputation: 2780

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.

Upvotes: 0

Views: 850

Answers (2)

theMayer
theMayer

Reputation: 16167

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.

Upvotes: 0

Lews Therin
Lews Therin

Reputation: 10995

Socket.IsBound doesn't check if the Socket is connected. It checks if a Socket is bound to a local port number. So no. There could be something you are doing wrong else where.

Supporting doc

To check if a Socket is connected Use Socket.Connected

Upvotes: 2

Related Questions