I am working on a TcpClient Asynchronous requesting WPF project. In which I have written some code when ever client gets disconnected with out network lost due to socket disconnected. Its working fine, and when the socket is removed/unplugged also on the server side no event is firing or no exception recieved to make the client status disconnected. I have seen about this issue in many sites, every where people used to say to use the Socket.Poll method but which is not working in my case. The SelectMode.SelectRead is always returning true when the client connected or disconnected, so I cant find any alternative.
So, I am posting my question here to know how to find when a client socket is unplugged/network access has been lost in asynchronous mode. I have tried with following code, any how its not working.
void CheckSocketStatus()
{
try
{
while (true)
{
System.Threading.Thread.Sleep(1000);
if(!mobjClient.Client.Poll(0,SelectMode.SelectWrite))
{
break;
}
}
}
catch(SocketException soex)
{
}
}
Hope someone can help me.
It is correct that this cannot be used to detect certain connection problems as by the documentation that states:
You should probably consider not relying on a persistent connection and instead open/close it on demand. Nevertheless, if you need some kind of connection state information, the unfortunate conclusion is that the underlying architecture does not provide it. You’ll have to do it by periodically exchanging packets and catching errors.
This is also what the documentation on the Connected property of TcpClient suggests: