Created
April 7, 2015 00:36
-
-
Save kazuho/e614ca99ab6699c37346 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use IO::Socket::INET; | |
sub can_bind { | |
my $port = shift; | |
!! IO::Socket::INET->new( | |
Listen => 5, | |
LocalAddr => '127.0.0.1', | |
LocalPort => $port, | |
Proto => 'tcp', | |
ReuseAddr => 1, | |
); | |
} | |
sub empty_port { | |
for (my $port = 49152; ; ++$port) { | |
if (can_bind($port)) { | |
warn "found empty_port: $port, the socket should have been closed now that we are out of can_bind"; | |
return $port; | |
} | |
} | |
} | |
my $s = IO::Socket::INET->new( | |
Listen => 5, | |
LocalAddr => '127.0.0.1', | |
LocalPort => empty_port(), | |
Proto => 'tcp', | |
ReuseAddr => 1, | |
) or die "could not bind to the returned port:$!"; | |
print "ok\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment