views:

494

answers:

3

hey. im writing a multithreaded winsock application and im having some issues with closing the sockets. first of all, is there a limit for a number of simultaneously open sockets? lets say like 32 sockets all in once.

i establish a connection on one of the sockets, and passing information and it all goes right. problem is when i disconnect the socket and then reconnect to the same destination, i get a RST from the server after my SYN. i dont have the code for the server app so i cant debug it.

when i used SO_LINGER and it sent a RST flag at the end of each session - it worked. but i dont want to end my connections this way. when not using SO_LINGER a FIN flag was sent but it seems the connection was not really closed.

any help? thanks

+1  A: 

On Unix there's a file descriptor limit per process - I'm guessing on Windows it's "handles".

You are probably bind()-ing your client socket to a fixed port. That might be the reason the server is rejecting your subsequent connection. Try normal ephemeral ports.

Nikolai N Fetissov
A: 

To disconnect a socket on a variety of legacy Unix systems, all different, I first clear any errors using SO_ERROR, then call shutdown(), and finally close(). But I don't know about WINSOCK.

Joseph Quinsey
A: 

Firstly, I agree with Nikolai, are you binding your client socket?

If so it sounds like the socket on the server side is still in TIME_WAIT and is discarding the new connection attempt. By binding the client socket you're forcing the server to try and reuse the exact same connection that is currently in the 2MSL wait period, it can't be reused at this point in time and so you're seeing what you're seeing. There's usually no need to bind the client port, stop doing it and your problem will likely go away.

Secondly, yes, there are limits to the number of open sockets on Windows platforms but they're resource related rather than some hard coded number.

Each open socket uses some 'non paged pool' memory and each pending read or write request on a socket is also likely to use both 'non paged pool' and have pages of memory locked in memory during I/O (there's a limit to the number of pages that can be locked). That said on Vista and later there's much more 'non paged pool' available than on earlier versions of Windows and even then I've managed to achieve more than 70,000 concurrent active connections on a pretty low spec XP box (see here: http://www.lenholgate.com/archives/000568.html). Note that there are some separate limits on the number of outbound connections that you can establish (which is more likely to be of interest to you) but that's around 4000 by default and can be tuned by setting MAX_USER_PORT see here: http://stackoverflow.com/questions/1536550/maximum-number-of-concurrent-tcp-ip-connections-win-xp-sp3/1537561#1537561 for more details.

Len Holgate
forgot to mention one thing.this issue is not occurring when i run it single-threaded - it all connects right.when i simultaneously run 2 and above sending threads(each opening 16 different sockets for sending to 16 different servers), somewhere it fails connecting - it varies.
Johnny Walked
ARE you binding the client sockets?
Len Holgate