pg has asked for the wisdom of the Perl Monks concerning the following question:

Yesterday when I was working on Re: Problems with select() on socket, I used this piece of code to test against it: (I am using win98 + Perl 5.8.0)

use IO::Socket::INET; use IO::Select; use strict; use warnings; my @sockets; my $select = new IO::Select(); my $packet_rcvd; for ($ARGV[0] .. $ARGV[1]) { my $socket = new IO::Socket::INET(Proto => "udp", LocalPort => $_, + LocalAddr => "localhost") || die "failed to establish socket for por +t $_"; print "udp socket created for port $_\n"; $select->add($socket); } print "all socket created\n"; while (1) { my @sockets = $select->can_read(10); for my $socket (@sockets) { my $peer = $socket->recv($packet_rcvd, 50); print "packet $packet_rcvd recvd at port " . $socket->sockport +() . "\n"; $socket->send("abcdefg", 0, $peer); } print $select->count(), "\n"; }

But I noticed that, if I have more than 64 ports open, only the first 64 ports actually receive packets, and response.

The script didn't die on socket establishment, so seems ports really got opened, (or failed but Perl didn't report?).

This is a per process limitation, if I run two instances of this script, each will have 64 ports response properly.

This is probably even not a Perl issue (OS?). How can I increase number of open ports allowed (on win98)?

Update:

The passage BrowserUK found explains this.

Changed the way of using select, and tried with this piece of code:

use IO::Socket::INET; use IO::Select; use strict; use warnings; my @sockets; my $packet_rcvd; for (4000 .. 4200) { my $socket = new IO::Socket::INET(Proto => "udp", LocalPort => $_, + LocalAddr => "localhost") || die "failed to establish socket for por +t $_"; print "udp socket created for port $_\n"; push @sockets, $socket; } print "all socket created\n"; for my $socket (@sockets) { my $select = new IO::Select($socket);#now select is only for one s +ocket, obviously less than 64 my @readable; do { @readable = $select->can_read(1); } until ($#readable != -1); my $peer = $socket->recv($packet_rcvd, 50); print "packet $packet_rcvd recvd at port " . $socket->sockport() . + "\n"; }

Now 125 ports responsed. But this 125 again seems to be some sort of limit.

Any way, if one wants to get around the 125 limitation, can just go multi-process.

select is obviously part of the problem for 64 limit. The first part in the passage provided by BrowserUK cannot be independently tested, as the implementation might not even use those mentioned function calls underneath.

Replies are listed 'Best First'.
Re: maximum number of open ports per process on win98
by BrowserUk (Patriarch) on Nov 27, 2003 at 04:47 UTC

    I think that this is the relevant passage from the winsock FAQ

    4.9 - What are the "64 sockets" limitations? There are two limitations in Winsock where you're limited to 64 socket +s. The Win32 event mechanism (e.g. WaitForMultipleObjects()) can only wai +t on 64 event objects at a time. Winsock 2 provides the WSAEventSelec +t() function which lets you use Win32's event mechanism to wait for e +vents on sockets. Because it uses Win32's event mechanism, you can on +ly wait for events on 64 sockets at a time. If you want to wait on mo +re than 64 Winsock event objects at a time, you need to use multiple +threads, each waiting on no more than 64 of the sockets. The select() function is also limited in certain situations to waiting + on 64 sockets at a time. The FD_SETSIZE constant defined in winsock. +h determines the size of the fd_set structures you pass to select(). +It's defined by default to 64. You can define this constant to a high +er value before you #include winsock.h, and this will override the de +fault value. Unfortunately, at least one non-Microsoft Winsock stack +and some Layered Service Providers assume the default of 64; they wil +l ignore sockets beyond the 64th in larger fd_sets. You can write a test program to try this on the systems you plan on su +pporting, to see if they are not limited. If they are, you can get ar +ound this with threads, just as you would with event objects.

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Hooray!
    Wanted!

Re: maximum number of open ports per process on win98
by noslenj123 (Scribe) on Nov 27, 2003 at 18:05 UTC
    I ran into the same sorts of limitations earlier this year while trying to write a simulation server for testing code at work. I ended up trying several things including arrays of select objects under 5.6 and pure blocking sockets in their own threads under 5.8. I tried it under both linux and Win2k with about the same results.

    Anytime I got past the 64 limits I seem to run into another limit around 110-125 or so. I did not try a select object per thread under 5.8 and perhaps when there's time I will. However, I need to service about 1000 sockets simultaneously and just couldn't get it to work very well even at around 100, performance dropped off. Unfortunately I ended up doing it in java which is working pretty well. For everything else I do, Perl rocks!

    Perhaps Perl 6.? will have a better setup for this issue.

      I ran into the exact same results and limitations myself. Ran some test on Win2k with the latest ActiveState Python 2.3.2 build. It has its limit at 512 sockets on a select. using its multi-threading, I got up to 2000 threads using blocking sockets. I could not create any more threads at that point. a non-blocking multiplexing approach using arrays of 512 sockets using select got me 4000 sockets ok. That is where I stopped.