I have an Asynchronous Client Socket and I want to determine whether or not I'm still connected. I have this function below that determines if I'm connected; however, it doesn't always work. After I connect, this function tells me correctly that I'm connected. If I kill the other program that this AsyncClient is talking to, this funciton tells me correctly that I'm disconnected. But if I disconnect the internet by pulling the plug and/or disconnecting the wifi, it does not tell me that I've been disconnected. Any ideas why? Any ideas how I'm supposed to check for connection status? Is there an event that tells me that I'm missing?
The code I'm using is referenced almost word for word from here
My Function to determine whether or not I'm connected (reference)
public static bool SocketConnected(Socket s)
{
if ( s != null )
{
bool part1 = s.Poll(1000, SelectMode.SelectRead);
bool part2 = (s.Available == 0);
Console.WriteLine("SocketConnected part1 = "+ part1 + " part2 = "+ part2 + " socket.Connected =" + s.Connected);
if ((part1 && part2 ) || !s.Connected)
return false;
else
return true;
}
else
{
Console.WriteLine("SocketConnected Socket is null");
return false;
}
}
Console Output:
Output I see when connected:
SocketConnected part1 = False part2 = True socket.Connected =True
Output I see after stopping the python program I'm connected to
SocketConnected part1 = True part2 = True socket.Connected =True
You can see that part1 flips to true...
Output I see when connected:
SocketConnected part1 = False part2 = True socket.Connected =True
Output I see after pulling plug on the internet:
SocketConnected part1 = False part2 = True socket.Connected =True
You can see that part1 is still false, and socket.Connected is still true... So this function has no knowledge that I've lost an internet connect.
Dropbox link to example project. Please note that you might not be able to connect to my server over the internet (if I have it down while testing) by running the code, but you're welcome to look at the code. TomTestViewController.cs & AsyncClient.cs have everything important.