Skip to content

Instantly share code, notes, and snippets.

@jrgm
Created January 31, 2013 07:16
Show Gist options
  • Save jrgm/4680965 to your computer and use it in GitHub Desktop.
Save jrgm/4680965 to your computer and use it in GitHub Desktop.
grab sockets using specific range of local ports all at once; how many in range are available (wait a couple of minutes before checking again on the same range, because obviously that range will be in time_wait).
use strict;
use IO::Socket::INET;
sub socketConnect {
my ($localPort) = @_;
my $err;
my $sock = IO::Socket::INET
->new(PeerAddr => '127.0.0.1',
PeerPort => '63300',
LocalAddr => 'localhost',
LocalPort => $localPort,
Proto => 'tcp') || ($err = $!);
if ($err) {
print STDERR "localPort: $localPort :", $!, $/;
return;
}
return unless $sock;
print STDERR join(' ',
$sock->sockhost, $sock->sockport,
$sock->peerhost, $sock->peerport), $/;
return 1;
}
my $start = 32768;
my $end = $start + 10000 - 1;
my $range = $end - $start;
my ($good, $bad, $total) = (0, 0, 0);
for my $i (0 .. $range) {
$total++;
if (socketConnect($start + $i)) {
$good++;
} else {
$bad++;
};
}
print "($good, $bad, $total)\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment