Socketexception 10024 Bug or Feature?

Hi all

During a stresstest on my TCP library i found and very stranged bug.

I send data from my Spider to my PC via TCP.

I wrote a simple loop and 127 times everything is fine but every time the 128 new Socket(…) fails.

The Errorcode is 10024

Too many open files.
Too many open sockets. Each implementation may have a maximum number of socket handles available, either globally, per process, or per thread.


I’m using a using block i checked the Garbage collector and I’m sure that Dispose is called.
I also tried to do the comminication in a thread but same result :frowning:

Here a simple code to reproduce:

            Debug.Print("Program Started");
            string host = "";
            int port = 5010;
            byte[] buffer = new byte[1];
            buffer[0] = 0xAA;
            int count = 0;

            while (true)

                    using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                        s.Connect(new IPEndPoint(IPAddress.Parse(host), port));
                    System.Threading.Thread.Sleep(1000); //Does not matter if i sleep 10 or 1000 ms
                catch (Exception ex)
                    Debug.Print(ex.Message); //127 Times everything ok 128. try crashes :(

if this is true i can only use 127 Sockets in my application ?

Search this forum for linger -2

You can have as much as 127 opened networking TCP sockets. Please remember that this is not a PC nor a server with limitless memory and processing resources.
127 sockets is considers very high number in embedded system world which usually accept systems with 4 to 16 sockets only.

Gus, If you take a look at the code, you will notice that does not even close the sockets. so lingering does not help. The code just opens sockets and allocate resources without releasing by closing the sockets.

Isn’t “using” will dispose sockets?

I am not sure is socket.dispose() closes the socket.

I found the linger 4 minutes after posting my question :slight_smile:

this was a not a real code. only a stress test to find such features for documentation
Is the Default linger time defined by .net micro Framework or by the hardware vendor?

@ Joe:
Close is called

this code

using (Socket s = new Socket(...))


is same as:


s.Dispose(); // and this call Close()


Reflectors says yes:

// System.Net.Sockets.Socket
/// <summary>Releases the unmanaged resources used by the <see cref="T:System.Net.Sockets.Socket" />, and optionally disposes of the managed resources.</summary>
/// <param name="disposing">true to release both managed and unmanaged resources; false to releases only unmanaged resources. </param>
protected virtual void Dispose(bool disposing)
	if (this.m_Handle != -1)
		this.m_Handle = -1;

Nice! thanks for the information. So you need to sent the lingering to -2 as Gus mentioned if you would like the socket resources to be release instantly after closing.

does .NET Gadgeteer use different SDK than EMX? Because on EMX when I use Socket(using System.Net.Sockets) I don’t have option to Dispose it…
for example this do not work:

Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
soc.Dispose(); //This do not exist

You can use using but I think it will not close socket and will leave it opened:

using (Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
    //do something with socket

So using will not work…

I recommend you that you using something like this:

Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        socket.Connect(new IPEndPoint(ip_address, port));
        //do something with socket...
    catch { }
    finally { socket.Close(); }

This will for shure Close you socket.Im try it right now and Im normaly open/close socket over 13000 times(Im make non blocking Ping function which try open socket every second test if PPP connection is online).
I didn’t setup any Lingering or something like that…

Here you are something to know about the using statement in .NET Framework:

Hope this helps.