Wiz5100 networking - protecting against socket depletion

in my app, I need to have several sockets in use. So I want to know how best to check I have sockets available - to protect the code from running out of sockets.

I will have a httplistener running on one socket; I’ll have (or at least I want to have) DHCP enabled, thats another socket down; and I periodically will need to do other network tasks (HTTP webservice push to remote servers, occasional NTP updates) that need to be able to access the network at different intervals.

Is the suggested approach purely wrap every socket creation step in try/catch blocks? Is there any other pre-emptive check i can do?

In particular I I’m still optimising my UDP network time code on

In the current implementation, all 4 sockets of W5100 can be used. So probably you can have a global variable that you increment when every you open a socket and decrement when you release it.

When you have DHCP enabled, then one socket will be always reserved for DHCP, so the maximum free-to-use sockets number is 3.
This socket will be used with DNS. so you don’t need to worry about these two services. Buy make sure to set the reserve flag to true in Wiznet.Enable().

Thanks Joe - it’d be great if we can identify if the WIZ chip can support reporting the # current sockets and adding a call that returns that somewhere in the future, and in the meantime I’ll just add a counter and delay actions if the counter shows the sockets would be over-consumed.

Edit: actually I have a related question. UDP for NTP, can you confirm I DO need to use two sockets, one bound to local EP and one to remote NTP server EP? I can’t see how it’d work any other way but if I can use one socket instead of two it improves my ability to handle multiple tasks at once

We will keep that in mind for future SDKs. :wink:

[quote]Edit: actually I have a related question. UDP for NTP, can you confirm I DO need to use two sockets, one bound to local EP and one to remote NTP server EP? I can’t see how it’d work any other way but if I can use one socket instead of two it improves my ability to handle multiple tasks at once
[/quote]

With UDP you nee donly one socket. here is an example

Socket mySocket = new Socket(ProtocolType.Udp , .....);

mySocket.SendTo(bytesToSend, len, SocketFlags.None, desEndPoint);

while (mySocket.Poll(2000000, SelectMode.SelectRead))
                {
                    if (mySocket.Available > 0)
                    {
                        byte[] inBuf = new byte[mySocket.Available];

                        EndPoint recEndPoint = null;
                        mySocket.ReceiveFrom(inBuf, ref recEndPoint);
                        if (!recEndPoint.Equals(desEndPoint))
                            continue;
                    }

                }

ok thanks Joe.

So in summary; no need to have two sockets; no need to bind those two sockets to the local and remote EPs; SendTo the remote EP; RecieveFrom and check the EP is who you expected.

Awesomeness

But you can still bind to a certain local port? You check the recEndPoint just to check the IP address of the remote IP address.