I have a .Net TCP client which connects to Telnet server (currently, for testing, via localhost):
tcpClient.ReceiveTimeout = 300;
tcpClient.SendTimeout = 300;
tcpClient.NoDelay = true;
LingerOption lingerOption = new LingerOption(true, 0);
tcpClient.LingerState = lingerOption;
tcpClient.Connect(address, port);
After connecting to the server the client cannot detect disconnection initiated by the server (e.g. server application has closed the socket or server process has terminated).
After server is closed I can see its FIN-ACK and client's ACK in Wireshark but both client socket and TcpClient object remain in connected state.
Adding keep-alive did not help - I keep getting keep-alive response after FIN-ACK.
The only thing that allowed me to detect server disconnection immediately is periodically writing to socket which is not acceptable in my case since it pollutes the connection with garbage.
Am I missing something in socket/client configuration?
When the server closes the connection gracefully (ie, sends a FIN
), the client socket will enter a readable state, and a subsequent read on that socket will report success with 0 bytes received. This is the typical way to detect socket closure.