System.Net.Sockets.SocketException

A lot of time, after I start my nic, I have that error every operation I try (for example a dns resolution).

I restart the project, maybe the board, and after one, two, or three times, code runs.

Any idea? :frowning:

thanks

my code (the exeption is raised executing GetHostEntry, but is same if I open a socket, I try and http connection, and so on):


Dim eth1 = interfaces(0)

Dim r As New Random
Dim mac(48 - 1) As Byte ' = {&H0, &H88, &H50, m1, m2, m3}
r.NextBytes(mac)
eth1.PhysicalAddress = mac

Thread.Sleep(100)

Dim netConfiguration = _sd.GetConfigurationNetwork()
eth1.EnableStaticIP(netConfiguration.Address, netConfiguration.Subnet, netConfiguration.Gateway)

eth1.EnableStaticDns(New String() {"208.67.222.222", "208.67.220.220"})

Thread.Sleep(100)

Dim d = System.Net.Dns.GetHostEntry("www.google.com")

IsNetworkRunning = (d IsNot Nothing AndAlso d.AddressList.Length > 0) 

cerbuino
it was created as a gadgeteer one, but in fact I’m working as in a standard MF and not accessing gadgeteer features

I lost first row in preview post:

   Dim interfaces() = Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces

Sleep was inserted just as test, meaning that maybe there was some delay in configuration startup in the board.
Is not important, the problem is same with or without.

What do you mean with “work with the network up and down event handlers”? Where are these events defined?

thanks

Thank you. I’ve tried to refactor code as you suggested, but in fact still the problem is the same.
Basically now I do:

1 - at startup:


            AddHandler Microsoft.SPOT.Net.NetworkInformation.NetworkChange.NetworkAvailabilityChanged, AddressOf NetworkAvailabilityChanged
            AddHandler Microsoft.SPOT.Net.NetworkInformation.NetworkChange.NetworkAddressChanged, AddressOf NetworkStateChanged
            IsNetworkRunning = False

2 - then


 Private Sub NetworkAvailabilityChanged(sender As Object, e As Microsoft.SPOT.Net.NetworkInformation.NetworkAvailabilityEventArgs)

            IsNetworkRunning = e.IsAvailable

            If e.IsAvailable Then

                NTPTime.UpdateTimeFromNTP("tempo.ien.it", 0)

                Dim d = System.Net.Dns.GetHostEntry("www.google.it")

                IsNetworkRunning = (d IsNot Nothing AndAlso d.AddressList.Length > 0)

            End If

        End Sub

The event is raised when I connect the cable, but then sometimes code go on, sometimes I’ve socket errors when I try to complete NTP or GetHostEntry… :frowning:
An unhandled exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll

Yes.
However is the NetworkAvailabilityChanged event that is raised. With the code modified I’ve this output:

Program Started
netav: 172.30.98.166
netav: 172.30.98.166
netav: 172.30.98.166
netav: 172.30.98.166


 Private Sub NetworkAvailabilityChanged(sender As Object, e As Microsoft.SPOT.Net.NetworkInformation.NetworkAvailabilityEventArgs)

            '    IsNetworkRunning = e.IsAvailable

            'If e.IsAvailable Then

            '    ' -- tiro su l'ora da NTP su UTC
            '    NTPTime.UpdateTimeFromNTP("tempo.ien.it", 0)

            '    ' -- testo la risoluzione DNS che servirĂ  dopo per vedere se ho connettivitĂ  funzionante
            '    Dim d = System.Net.Dns.GetHostEntry("service.cloudmo.net")

            '    IsNetworkRunning = (d IsNot Nothing AndAlso d.AddressList.Length > 0)

            'End If

            Debug.Print("netav: " & Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces(0).IPAddress)

        End Sub

        Private Sub NetworkStateChanged(sender As Object, e As Microsoft.SPOT.EventArgs)

            Debug.Print("ipchan: " & Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces(0).IPAddress)


        End Sub

ip and network start seems ok, are sockets that have problem :frowning: