TCP Server Error

Hello
I’ve wrote a simple code for TCP server on my raptor :


 private bool InitEthernet()
        {
            if (!ethernet_ENC28.Interface.IsOpen)
            {
                ethernet_ENC28.Interface.Open();
            }
            NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet_ENC28.Interface);
            ethernet_ENC28.Interface.NetworkInterface.EnableStaticIP(DeviceIP, SubnetMask, DefultGateway);
            ethernet_ENC28.DebugPrintEnabled = true;
            string res = TCPHandler();
            if (res == "True")
                return true;
            else
            {
                Error = res;
                return false;
            }
        }

private string TCPHandler()
        {
            byte[] bytes = new Byte[1024];
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, SocketPort);
            bool recieved= false;
            string res = string.Empty;
            listener.Bind(localEndPoint);
                listener.Listen(1);
                Socket handler = null;
                try
                {
                while (!recieved)
                {
   
                handler = listener.Accept();
                recieved = true;
                }
                byte[] clientData = new byte[1024 * 100];
                int receivedBytesLen = handler.Receive(clientData);
 return "True";
                
            }
            catch (Exception ex)
            {
                return ex.Message.ToString(); 
            }
        }


but I get this error :


Exception was thrown: System.Net.SocketException

on this line :


 handler = listener.Accept();

Why !!!
can everybody help?
thanks

@ andre.m -
firmware : 4.2
and exception : System.Net.SocketException

@ Ehsan Ansari - Can you switch to DHCP and put a little wait loop and print out the IP settings, just to make sure you have IP working at all.

Something like this, the code does run on my raptor with an enc28 1.1 module:

            // Fire up the events
            NetworkChange.NetworkAvailabilityChanged += (a, b) => Debug.Print(DateTime.Now + " Network availability = " + b.IsAvailable.ToString());
            NetworkChange.NetworkAddressChanged += (a, b) => Debug.Print(DateTime.Now + " Network address change ");

            // Start the network
            netif = new EthernetENC28J60(SPI.SPI_module.SPI2, (Cpu.Pin)37, (Cpu.Pin)32, (Cpu.Pin)7, 24000);
            netif.Open();
            netif.EnableDhcp();
            netif.EnableDynamicDns();

            // Waiting for an address
            while (netif.IPAddress == "0.0.0.0")
            {
                Debug.Print(DateTime.Now + " Waiting for network address ...");
                Thread.Sleep(250);
            }

            // Some network stats
            Debug.Print(DateTime.Now + " New address for the Network Interface ");
            Debug.Print(DateTime.Now + "  Is DHCP enabled: " + netif.NetworkInterface.IsDhcpEnabled);
            Debug.Print(DateTime.Now + "  Is DynamicDnsEnabled enabled: " + netif.NetworkInterface.IsDynamicDnsEnabled);
            Debug.Print(DateTime.Now + "  NetworkInterfaceType " + netif.NetworkInterface.NetworkInterfaceType);
            Debug.Print(DateTime.Now + "  Network settings:");
            Debug.Print(DateTime.Now + "   IP Address: " + netif.NetworkInterface.IPAddress);
            Debug.Print(DateTime.Now + "   Subnet Mask: " + netif.NetworkInterface.SubnetMask);
            Debug.Print(DateTime.Now + "   Default Gateway: " + netif.NetworkInterface.GatewayAddress);
            Debug.Print(DateTime.Now + "   MAC Address: " + GetMACAddress(netif.NetworkInterface.PhysicalAddress));
            Debug.Print(DateTime.Now + "   Number of DNS servers: " + netif.NetworkInterface.DnsAddresses.Length);
            for (int i = 0; i < netif.NetworkInterface.DnsAddresses.Length; i++)
                Debug.Print(DateTime.Now + "     DNS Server " + i.ToString() + ":" + netif.NetworkInterface.DnsAddresses[i]);
            Debug.Print(DateTime.Now + " ------------------------------------------------------");

The settins are valid for connecting a ENC28 to socket 1 of the Raptor.

Edit: this is done in plain NetMf so without using the gadgeteer designer !!!

The output looks like this:

@ Ehsan Ansari - I extended your TcpHandler() routine to return the received client data as well.

And it did run just fine on my Raptor with enc28. The raptor is on firmware 4.3.6.0 and the code is run by VS2013.

Maybe you should consider getting the latest SDK. Here’s how .NET Micro Framework - GHI Electronics


        private static string TCPHandler()
        {
            byte[] bytes = new Byte[1024];
            Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 80);
            bool recieved = false;
            string res = string.Empty;
            listener.Bind(localEndPoint);
            listener.Listen(1);
            Socket handler = null;
            try
            {
                while (!recieved)
                {

                    handler = listener.Accept();
                    recieved = true;
                }
                byte[] clientData = new byte[1024 * 100];
                int receivedBytesLen = handler.Receive(clientData);
                if (receivedBytesLen == 0) return "Nothing!!";
                
                res = "";
                for (int i = 0; i < receivedBytesLen; i++)
                {
                    res += (char)clientData[i];
                }
                return res;

            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }

With this output when I browse with my wifi connected android mobile phone:

Hope this helps.

@ andre.m -
sorry for delay… I was in holiday
the error is : 10050 which is related to down network! but it cannot be correct because I ran previous version of my code so that the raptor was TCP client and my PC was TCP server on current network…

@ PiWi -
I switched to DHCP and it was amazing:


06/01/2011 00:20:02 Waiting for network address ...
06/01/2011 00:20:02 Waiting for network address ...
06/01/2011 00:20:03 Waiting for network address ...
06/01/2011 00:20:03 Waiting for network address ...
06/01/2011 00:20:03 Waiting for network address ...
06/01/2011 00:20:03 Waiting for network address ...
06/01/2011 00:20:04 Waiting for network address ...
06/01/2011 00:20:04 Waiting for network address ...
06/01/2011 00:20:04 Waiting for network address ...
06/01/2011 00:20:04 Waiting for network address ...
06/01/2011 00:20:05 Waiting for network address ...
06/01/2011 00:20:05 Waiting for network address ...
06/01/2011 00:20:05 Waiting for network address ...

DHCP cannot make valid IP…
it seems to be related to firmeware version. I will test it

@ Ehsan Ansari - look here for more on using TCP Server or even a UDP Server …

I’ve found the network getting more stable with the latest FW updates. Watch out for the use of the gadgeteer webserver that still has a little flaw and won’t work, one still needs to manually fix, at least I had to do it still on 2.43.1.0 … :frowning:

1 Like