Re: Maximum sockets

From: Phil Frisbie, Jr. (phil_at_hawksoft.com)
Date: 02/09/05


Date: Wed, 09 Feb 2005 15:45:33 -0800

Chad wrote:

> Essentially, my question is this:

I already saw this multiposted to another group earlier, but I guess I can
answer it here now that I have the time...

> What is the maximum number of sockets allowed (in any
> state, ie:
> ESTABLISHED, CLOSE_WAIT, FIN_WAIT_2, etc) per thread? per
> process?
> globally?

There is no hard coded limit on NT based OSs. However...

> The reason for this question is that a client application
> that I wrote,
> which makes many separate and short connections to a server,
> occasionally receives a WSAEADDRINUSE error message after
> attempting a
> connect(). As I understand it, this message must be
> arising because a
> local port that my socket is binding to is already being
> used. Since I
> tell Windows to allocate the port itself, this should never
> happen.

This is not a socket problem, it is a port problem.

> Looking at one "netstat" snapshot during runtime, I notice
> that there
> are approximately 377 sockets in use globally, and my
> application is
> using 270 sockets (20 of those 270 sockets are in an
> ESTABLISHED state,
> the rest are in a CLOSE_WAIT state).

Yes, you found your problem, even if you did not understand that answer! When
you call connect() Windows assigns it to a unused port. The problem is that by
default Windows only uses the ports from 1024 through 5000. When you close a TCP
socket, it stays in the CLOSE_WAIT for about two minutes, so you can only
connect and close less than 3976 sockets every two minutes.

> I am wondering if 270 sockets per process is a maximum for
> Windows 2000
> Professional. If that is so, then when I perform a
> connect(), Windows
> might be unable to grant additional sockets, so it must grant a
> pre-existing socket (which is where the WSAEADDRINUSE
> message is coming
> from). At least, this is my theory. I hope somebody has
> some actual
> evidence of any socket limits.

You can connect more than 8000 sockets on Windows 2000 Pro with 256 MB of RAM,
you just need to get around the 'port 1024 through 5000' limitation Windows uses.

You can either make a registry change. A news group search will turn up that
info, or you can use a socket library that bypasses the limitation. I have one
on my website below that if free to use, or you can just see how I did it and
write your own code.

> It has been difficult to find relevant answers to this
> question, as
> most descriptions of the WSAEADDRINUSE error message
> describe servers
> that are listening on a port already in use. In my case,
> Windows
> should be automatically choosing a free port, and
> apparantly it is not.

The 'magic' phrase is 'ephemeral port range'.

> Any help would be greatly appreciated.

-- 
Phil Frisbie, Jr.
Hawk Software
http://www.hawksoft.com


Relevant Pages

  • RE: call is blocked in recvfrom() and no further proceedings in Win CE
    ... In windows CE, I'm able to send a request but I'm unable to receive it. ... Create another socket & bind with server IP address. ... > My program has to send request to service through port 5070(in this port only ...
    (microsoft.public.windowsce.embedded)
  • Re: call is blocked in recvfrom() and no further proceedings in Win CE
    ... > In windows CE, I'm able to send a request but I'm unable to receive it. ... Create another socket & bind with server IP address. ... >> My program has to send request to service through port 5070(in this port ...
    (microsoft.public.windowsce.embedded)
  • Re: call is blocked in recvfrom() and no further proceedings in Wi
    ... How is the Windows CE device connected to the network where the desktop ... that won't work with UDP. ... >>> My program has to send request to service through port 5070(in this ... >>> socket ...
    (microsoft.public.windowsce.embedded)
  • Re: Socket application under Windows XP
    ... > if I attempt to run this application under Windows XP I ... Is the application creating a socket and then binding it to a specific port ... If so then maybe that port is already by something else on XP. ...
    (microsoft.public.windowsxp.network_web)
  • Maximum sockets
    ... What is the maximum number of sockets allowed (in any ... occasionally receives a WSAEADDRINUSE error message after ... tell Windows to allocate the port itself, ...
    (microsoft.public.win32.programmer.networks)

Loading