ソケットのテストのためだけに書き込みを試みることはお勧めできません。また、.NETのConnectedプロパティでもリレーしないでください。
あなたは、リモートエンドポイントがまだアクティブである場合、あなたは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();
関連項目:TcpState状態のMSDN
説明にMSDN
IPGlobalPropertiesに
TcpConnectionInformationを
Netstat 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;
}
うん。ソケットレイヤーは例外を使用して管理されます。 IOExceptionがスローされるリモートタイムアウトまたは閉鎖ソケットを検出するのに必要なすべての情報が含まれているのSocketExceptionに設定内部例外を有しています。 – Luca