use IO::Socket::INET; use strict; use constant MAX_CONN => 25; $| ++; my @connections; my $request = "HEAD / HTTP/1.1\r\nConnection: Keep-Alive\r\nHost: www.google.com\r\n\r\n"; for (1..MAX_CONN) { $connections[$_] = new IO::Socket::INET(proto => "tcp", PeerAddr => "www.google.com", PeerPort => 80); print "."; } print "\n"; while (1) { my $sum = 0; for (1.. MAX_CONN) { my $buffer; if ($connections[$_]) { $sum ++; $connections[$_]->send($request); $buffer = ""; while ($buffer !~ m/\r\n\r\n/) { my $piece; sysread($connections[$_], $piece, 10000); if ($piece eq "") { $connections[$_]->close(); $connections[$_] = undef; last; } else { $buffer .= $piece; } } } else { #get those disconnected back $connections[$_] = new IO::Socket::INET(proto => "tcp", PeerAddr => "www.google.com", PeerPort => 80); } } print "there are $sum connections now\n"; }