Best Answer icabodcam, 28 April 2015 - 05:54 PM
I believe that I've got a solution.
Reminded in this thread by Nobby, the Socket.Poll method was the solution.
So the solution looks something like this in the constructor for the listener class:
try { Debug.Print(NetworkInterface.GetAllNetworkInterfaces()[0].IPAddress); listeningSocket.Bind(new IPEndPoint(IPAddress.Any, portNumber)); listeningSocket.Listen(10); bool testConnection = listeningSocket.Poll(500,SelectMode.SelectRead); } catch (SocketException) { isNetworkAvailable = false; listeningSocket.Close(); }
Then in the method or thread that handles the listener, test the status of isNetworkAvailable before using the socket, kind of like:
if (isNetworkAvailable) { try { using (Socket clientSocket = listeningSocket.Accept()) { ... }
...and in the
NetworkAvailabilityChanged handler if network not available then close the socket, else recreate the socket, bind, and start listening.
I hope that helps someone else.
Go to the full post