Is it possible to determine if a socket is in CLOSE_WAIT state?

I use the TcpClient object to send and receive data. When a client loses its connection, I see, using TcpView, that its connected connection is in the CLOSE_WAIT state. Is it possible to see socket status in C #? I just want to detect the CLOSE_WAIT state CLOSE_WAIT that I can close the socket on the server. I understand that I can wait for bytes to be received for a certain time and then close the connection if nothing is received. I would rather close the socket and its stream immediately. Is it possible to determine the state of a socket on the C # side? TcpView can detect this. Can a C # program do this?

+1
source share
1 answer

This is not ideal, but if you check socket.Available , it will throw a SocketException . However, you still need to check the reason.

Also not perfect, because it's a bit awkward, you need to call Poll() (using SelectRead ). If it returns true , either the data to read, or the connection is closed. If there is no data to read, then your answer.

0
source

Source: https://habr.com/ru/post/1401148/


All Articles