0

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.

Community
  • 1
  • 1
LampShade
  • 2,675
  • 5
  • 30
  • 60
  • If I remember correctly you should get an exception in EndRead method or it ends with 0 bytes. – SKall Mar 06 '14 at 22:30
  • Have you tried [`KeepAlive` option](http://msdn.microsoft.com/en-us/library/system.net.sockets.socketoptionname(v=vs.110).aspx)? – noseratio Mar 07 '14 at 00:12
  • I put "mySocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);" into my code, and I'm not seeing any differences. Still trying to determine how to correctly use/enable the KeepAlive though – LampShade Mar 07 '14 at 15:50
  • I'm not getting any exceptions when I pull the plug on the internet. I'm attaching my code via dropbox link – LampShade Mar 07 '14 at 15:52
  • I had to implement my own keep alive, because I couldn't get this one working... – LampShade Mar 10 '14 at 15:57

0 Answers0