Main Site Documentation

Getting SocketException ErrorCode = 10050


#1

Sometimes, but not every time, when I start the Ethernet system, I get this exception:

Exception System.Net.Sockets.SocketException - CLR_E_FAIL (14)

#### Message: 
#### Microsoft.SPOT.Net.SocketNative::poll [IP: 0000] ####
#### System.Net.Sockets.Socket::Poll [IP: 0011] ####
#### System.Net.Sockets.Socket::Accept [IP: 0017] ####
#### SocketServer.TCPServer::ProcessServer [IP: 0041] ####
#### SocketException ErrorCode = 10050
#### SocketException ErrorCode = 10050

A first chance exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = 10050
#### SocketException ErrorCode = 10050
2014/08/30 08:15:34.037 SocketServer.ProcessServer( tcp port = 8005 ): exception: Exception was thrown: System.Net.Sockets.SocketException

I am using MF 4.3. Here is my Ethernet startup code:



        /// <summary>
        /// Start the Client TCP/IP network software
        /// </summary>
        /// <returns>true if TCP/IP started ok</returns>
        private bool StartClient()
        {
            bool rc = false;
            Debug.Print("Initializing ENC28J60 ethernet system");
            NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(eth_NetworkAddressChanged);
            NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(eth_CableConnectivityChanged);
            
            //GHI.Premium.System.Util.SetMACAddress(GHI.Premium.System.NetworkInterface.ENC28j60_Ethernet, netData.MacAddress);
            eth.PhysicalAddress = netData.MacAddress;
            Thread.Sleep(2000);
            if (!eth.Opened)
            {
                Debug.Print("Not opened - open ENC28J60 ethernet system");
                eth.Open();
            }
            if (!eth.CableConnected)
            {
                Debug.Print("Ethernet cable is not connected, waiting for connection");
                CableConnectedEvent.WaitOne();
                Thread.Sleep(1000);
            }
            ni = eth.NetworkInterface;
            if ( ni == null )
            {
                rc = false;
                return rc;
            }

            //report MAC address settings
            byte[] PhyAddress = netData.MacAddress;
            string sMAC = KioskUtilities.BytesToHexString(PhyAddress, PhyAddress.Length);
            Debug.Print("MAC address in flash memory = " + sMAC);
            PhyAddress = ni.PhysicalAddress;
            sMAC = KioskUtilities.BytesToHexString(PhyAddress, PhyAddress.Length);
            Debug.Print("MAC address in Network Interface = " + sMAC);
                 
            //set IP, Gateway, DNS IPs
            ni.EnableStaticIP(netData.StaticIP, netData.NetMask, netData.GatewayAddress);
            ni.EnableStaticDns(new string[] { netData.PrimaryDNS, netData.SecondaryDNS });

            if (eth.NetworkIsAvailable)
                Debug.Print("EthernetENC28J60 is activated");
            else
                Debug.Print("EthernetENC28J60 is shut down");

            if (eth.Opened)
                Debug.Print("EthernetENC28J60 interface is open and assigned to TCP/IP");
            else
                Debug.Print("EthernetENC28J60 interface is closed");

            if (eth.CableConnected)
                Debug.Print("EthernetENC28J60 cable is connected");
            else
                Debug.Print("EthernetENC28J60 cable is not connected");

            int waitCount = 60;
            bool niSet = false;
            while ( (waitCount > 0) && !niSet )
            {
                Thread.Sleep(1000);
                waitCount--;
                if ((ni.IPAddress == netData.StaticIP) && (ni.SubnetMask == netData.NetMask))
                    niSet = true;
            }

            Thread.Sleep(1000);

            if (niSet)
            {
                Debug.Print("EthernetENC28J60 IP address has been set: IP = " + ni.IPAddress + " Subnet mask = " + ni.SubnetMask);
                rc = true;
            }
            else
            {
                Debug.Print("Could not set EthernetENC28J60 IP address");
                rc = false;
            }
            return rc;
        }

        void eth_CableConnectivityChanged(object sender, NetworkAvailabilityEventArgs e)
        {
            try
            {
                if (e.IsAvailable)
                {
                    Debug.Print("PrototypeDevice.CableConnectivityChanged Event - Ethernet cable is connected");
                    CableConnectedEvent.Set();
                }
                else
                {
                    Debug.Print("PrototypeDevice.CableConnectivityChanged Event - Ethernet cable is not connected");
                }
            }
            catch (Exception ex)
            {
                KioskUtilities.LogException("PrototypeDevice.eth_CableConnectivityChanged() exception ", ex);
                KioskUtilities.Reboot();
            }
        }

        void eth_NetworkAddressChanged(object sender, EventArgs e)
        {
            try
            {
                Microsoft.SPOT.Net.NetworkInformation.NetworkInterface ni  = eth.NetworkInterface;
                Debug.Print("NetworkAddressChanged Event");
                if (ni.IsDhcpEnabled)
                    Debug.Print("DHCP is enabled");
                else
                    Debug.Print("DHCP is disabled");
                Debug.Print("IP address = " + ni.IPAddress);
                Debug.Print("Gateway address = " + ni.GatewayAddress);
                Debug.Print("Subnet mask = " + ni.SubnetMask);

                foreach (string address in ni.DnsAddresses)
                {
                    Debug.Print("DNS address  = " + address);
                }
            }
            catch (Exception ex)
            {
                KioskUtilities.LogException("eth_NetworkAddressChanged() exception ", ex);
                KioskUtilities.Reboot();
            }
        }



#2

10050 essentially is “network is down”.


#3

The exception happens when I call Socket.Accept(). It consistently happens after I use FEZConfig to erase the application, then start the program from Visual Studio. After I restart the program in Visual Studio, no more exceptions. Does this shed any light?


#4

@ dspacek - see issues at https://www.ghielectronics.com/support/netmf/sdk/19/netmf-and-gadgeteer-package-2014-r2
"-TCP connections can fail after the first time when using MFDeploy."


#5

Try power off and leave the board 30 seconds or more to see…

Some router/switch are not happy if they see “ethernet.Open()” twice within 30 seconds if same MAC address.


#6

I fixed the problem by changing my network startup code, instead of this:


 if (!eth.CableConnected)
             {
                 Debug.Print("Ethernet cable is not connected, waiting for connection");
                 CableConnectedEvent.WaitOne();
                 Thread.Sleep(1000);
             }

I have this:


                 Debug.Print("Ethernet cable is not connected, waiting for connection");
                 CableConnectedEvent.WaitOne();

The event gets set inside eth_CableConnectivityChanged() event handler.

It seems that eth.CableConnected property is saying “true” when it should be “false”


#7

@ dspacek -

In my opinion, it is still not right way. Following your code, this bug won’t be happened if use DHCP.

The best way is should wait until the event “NetworkChange_NetworkAddressChanged” is raised up.

if this event was not raised, you can not operate network even you see netif.IP as expected. The static IP was saved in flash and it may be loaded before done connecting.

But just my opinion, happy because you killed the issue. :smiley:


#8

my 1c worth

You need BOTH the network to be connected and an IP address before the network is really up. If you have a static address but you have not connected the cable, just because the IP address is set doesn’t mean you can communicate. There should be a way to alert (thru a console/display etc) that the network isn’t fully up, which would require you to be able to validly check the network is up and the IP is set.