Main Site Documentation

Socketexception 10024 Bug or Feature?


#1

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

[quote]
WSAEMFILE 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.

[quote]

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");
            ethernet.UseStaticIP("10.0.0.166","255.255.255.0","10.0.0.138");
            string host = "10.0.0.51";
            int port = 5010;
            byte[] buffer = new byte[1];
            buffer[0] = 0xAA;
            int count = 0;

            while (true)
            {
                try
                {

                    using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                    {
                        s.Connect(new IPEndPoint(IPAddress.Parse(host), port));
                        s.Send(buffer);
                    }
                    count++;
                    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 ?


#2

Search this forum for linger -2


#3

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.


#4

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.


#5

Isn’t “using” will dispose sockets?


#6

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


#7

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:


try
{

...
}finally
{
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>
[MethodImpl(MethodImplOptions.Synchronized)]
protected virtual void Dispose(bool disposing)
{
	if (this.m_Handle != -1)
	{
		SocketNative.close(this);
		this.m_Handle = -1;
	}
}



#8

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.


#9

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);
    try
    {
        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…


#10

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

http://msdn.microsoft.com/en-us/library/aa355056.aspx

Hope this helps.

Jay.