I would not recommend you try writing only for socket testing. And do not relay the .NET Connected property either.
If you want to know if the remote endpoint is 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) {
See also:
TcpConnectionInformation on MSDN
IPGlobalProperties on MSDN
Description of TcpState Approves
Netstat on Wikipedia
And here it is like 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; }
uriel Oct 31 '13 at 12:15 2013-10-31 12:15
source share