0

Is it the right way to detect TcpClient connection closed state, or do I have to write zero bytes? I know it may return true if the sockets is not closed gracefully at the other side. What I need is to detect closed sockets by Operating System.

static bool IsConnected(TcpClient tcpClient)
{
    if (tcpClient.Client.Poll(10, SelectMode.SelectError))
        return false;

    if (!tcpClient.Client.Poll(10, SelectMode.SelectWrite))
        return false;

    return true;
}
Mohammad Nikravan
  • 1,543
  • 2
  • 19
  • 22
  • @KlavsPeder No there was nothing about Socket.Poll. Also, I use keep-alive and I can see my connections are stuck in read but OS said they are in the closed state. I need a way to retrieve my socket status. – Mohammad Nikravan Dec 31 '21 at 08:35
  • Do these questions solve your problem [C# How to determine if the tcp is connected or not?](https://stackoverflow.com/questions/6993295/how-to-determine-if-the-tcp-is-connected-or-not)? and [C# Instantly detect client disconnection from server socket](https://stackoverflow.com/questions/722240/instantly-detect-client-disconnection-from-server-socket)? –  Dec 31 '21 at 14:43
  • Does this answer your question? [How to determine if the tcp is connected or not?](https://stackoverflow.com/questions/6993295/how-to-determine-if-the-tcp-is-connected-or-not) –  Dec 31 '21 at 16:40
  • @richardec I had seen that before. They target different problem, also no one talk about SelectMode.SelectError – Mohammad Nikravan Jan 05 '22 at 07:22

0 Answers0