Main Site Documentation

Strange things regarding sockets and ethernet connection


This is my code :

void ethernet_J11D_NetworkUp(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)

            Debug.Print("Network Up!");
            ipAddress = ethernet_J11D.NetworkSettings.IPAddress;
            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            System.Net.IPAddress ipaddressReady = System.Net.IPAddress.Parse(ipAddress);
            System.Net.IPEndPoint localEndPoint = new System.Net.IPEndPoint(ipaddressReady, port);
            while (true)
                    Socket clientSocket = server.Accept();
                    // Process the client request.  true means asynchronous.
                    new ProcessClientRequest(clientSocket, true);
                catch (System.Net.Sockets.SocketException e)

Everything works fine but sometimes the while true loop got stuck somehow and my gadgeteer won’t accept new connections. I’m trying to connect to the gadgeteer via sockets, I ran it on Visual studio 2010 and sometimes nothing happened. I decided to put a breaking point (while the program was running) on the while (true) line, Then it became yellow which means that the loop got stuck, I pressed play (to jump to the next command) and suddenly new connection was accepted.

What is this???


you have a loop inside an event handler. this is a BIG no no! all sorts of strange things can happen.

do your incoming connection acceptance in a separate thread, created in the network up event handler.


I changed it now, let’s hope for the best.


I did it, but it seems that after a sometime that there are no incoming connections, It goes to sleep and no new connections accepted because the while loop dies for some reason.


Dies? without seeing your code, I can only guess that there is an unhandled exception. add a catch(Exception), and print out the associated message.

how many connections have occurred before the problem appears? Often, improper closing/disposal of sockets results in the maximum number of sockets being used.

I suggest that you write, and post, a simple program which accepts connections, does dummy processing, and then terminates the connection. that will let us look at the entire situation.


There is a strange issue with NETMF (not sure if GHI related or not), when a debug session is open.
The program suddenly stops as if it ran on a break point.
If you press pause, and then continue in VS, the program runs again.
This does usually not happen on very simple projects. But as soon as a projects gets more complex (Networking, Threads, …) it happens more often.
If no debugger is attached, the program runs just fine without any issues.

And as Mike already posted:
Never do “real work” in an event handler, or in an timer event.
NETMF runs a lot of these events on a single thread, and as long as a event is executed, no other event that is processed on this one thread will be fired.
And also put a try/catch block in every event handler and thread proc. If a exception is not caught there, the whole program will crash and the board reboots.

I have created a ThreadPool class to simply spawn an async operation without creating new threads all the time.
You can find it on CodeShare.


@ Reinhard Ostermeier - This is what I’m talking about, So, if the visual studio not working, the gadgeteer should accept new connections anytime?


If no debug session is running (Board starts after power up or reset button) then the ‘Break’ like I meant did never happen to me, so it should accept connections at any time.
Only when debugging with VS I saw this imaginary breakpoints.


@ Reinhard Ostermeier - Has it occurred to you in VS 2010 or 2010?