Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

NetworkInterface.OperationalStatus

networking

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


  • Please log in to reply
1 reply to this topic

#1 icabodcam

icabodcam

    New Member

  • Members
  • Pip
  • 8 posts

Posted 28 April 2015 - 03:41 PM

I've got a project which provides sensor information through a web interface.  Basic stuff really, but I'm running into an issue where, through no fault of the netduino, I'm losing the network connection at the device.  The only current soluition is to reboot the device, and this is really impractical, so I'm looking for ways to handle this in the code.  I initially thought to reboot the device in the code if the network drops, but that's not great either because I can image a situation where it will get into a reboot loop.   

 

So, I've just added error handling using the NetworkAvailablityChanged event of the NetworkChange class of the Microsoft.SPOT.Net.NetworkInformation namespace.  This allows me to handle when the device has had connection but loses it temporarially.  The problem, though, is that the device has a static IP address.  I need a way to determine if it is connected to the network at boot up to prevent errors, and just checking that it's IP isn't 0.0.0.0 won't work.  

 

It appears that the OperationalStatus property isn't available for the NetworkInterface in the framework, so I'm looking for another idea. 

 

 



#2 icabodcam

icabodcam

    New Member

  • Members
  • Pip
  • 8 posts

Posted 28 April 2015 - 05:54 PM   Best Answer

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. 







Also tagged with one or more of these keywords: networking

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.