1-ケーブルが抜かれているためにクライアントが切断された場合、次のソケットの読み取りまたは書き込みまでわかりません。また、tcpClient.Connectedプロパティの値は信頼できず、最後の通信に応じた値であることに注意してください。したがって、最後の通信が成功した場合、その値はtrueであり、それ以外の場合はfalseです。詳細については、これを確認してください。
2-ネットワークストリームとクライアントを閉じると、これはクライアントの正常な終了です。
3-わからない、テストしてみてください。
ケーブルが抜かれているなどの理由で接続が失われていることに気付いた場合、適切なIsConnected値を取得するには、tcpへの読み取りまたは書き込み中に失われた接続に注意する必要があるため、試してみてtcpclientメンバーにアクセスする必要があります。 -その操作をキャッチ...
このIsConnectedプロパティを使用して、tcpClientが接続されているかどうかを確認します。
public static bool IsConnected
{
get
{
try
{
//return _tcpClient != null && _tcpClient.Client != null && _tcpClient.Client.Connected;
if (_tcpClient != null && _tcpClient.Client != null && _tcpClient.Client.Connected)
{
/* As the documentation:
* When passing SelectMode.SelectRead as a parameter to the Poll method it will return
* -either- true if Socket.Listen(Int32) has been called and a connection is pending;
* -or- true if data is available for reading;
* -or- true if the connection has been closed, reset, or terminated;
* otherwise, returns false
*/
// Detect if client disconnected
if (_tcpClient.Client.Poll(0, SelectMode.SelectRead))
{
byte[] buff = new byte[1];
if (_tcpClient.Client.Receive(buff, SocketFlags.Peek) == 0)
{
// Client disconnected
return false;
}
else
{
return true;
}
}
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
}
}