Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if TcpClient Connection is closed?

I'm playing around with the TcpClient and I'm trying to figure out how to make the Connected property say false when a connection is dropped.

I tried doing

NetworkStream ns = client.GetStream(); ns.Write(new byte[1], 0, 0); 

But it still will not show me if the TcpClient is disconnected. How would you go about this using a TcpClient?

like image 757
RoboDev Avatar asked Sep 07 '09 03:09

RoboDev


People also ask

How do I know if my TCP connection is closed?

Enter "telnet + IP address or hostname + port number" (e.g., telnet www.synology.com 1723 or telnet 10.17. xxx. xxx 5000) to run the telnet command and test the port status. If the port is open, a message will say Connected to 10.17.

What is net sockets TcpClient?

The TcpClient class provides simple methods for connecting, sending, and receiving stream data over a network in synchronous blocking mode. In order for TcpClient to connect and exchange data, a TcpListener or Socket created with the TCP ProtocolType must be listening for incoming connection requests.


2 Answers

I wouldn't recommend you to try write just for testing the socket. And don't relay on .NET's Connected property either.

If you want to know if the remote end point is still active, you can use TcpConnectionInformation:

TcpClient client = new TcpClient(host, port);  IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties(); TcpConnectionInformation[] tcpConnections = ipProperties.GetActiveTcpConnections().Where(x => x.LocalEndPoint.Equals(client.Client.LocalEndPoint) && x.RemoteEndPoint.Equals(client.Client.RemoteEndPoint)).ToArray();  if (tcpConnections != null && tcpConnections.Length > 0) {     TcpState stateOfConnection = tcpConnections.First().State;     if (stateOfConnection == TcpState.Established)     {         // Connection is OK     }     else      {         // No active tcp Connection to hostName:port     }  } client.Close(); 

See Also:
TcpConnectionInformation on MSDN
IPGlobalProperties on MSDN
Description of TcpState states
Netstat on Wikipedia


And here it is as an extension method on TcpClient.

public static TcpState GetState(this TcpClient tcpClient) {   var foo = IPGlobalProperties.GetIPGlobalProperties()     .GetActiveTcpConnections()     .SingleOrDefault(x => x.LocalEndPoint.Equals(tcpClient.Client.LocalEndPoint));   return foo != null ? foo.State : TcpState.Unknown; } 
like image 110
uriel Avatar answered Sep 21 '22 22:09

uriel


As far as I know/remember there is no way to test if a socket is connected other than reading or writing to it.

I haven't used the TcpClient at all but the Socket class will return 0 from a call to Read if the remote end has been shutdown gracefully. If the remote end doesn't shutdown gracefully [I think] you get a timeout exception, can't remember the type sorry.

Using code like 'if(socket.Connected) { socket.Write(...) } creates a race condition. You're better off just calling socket.Write and handling the exceptions and/or disconnections.

like image 44
Kepboy Avatar answered Sep 18 '22 22:09

Kepboy