Net::EmptyPort - find a free TCP/UDP port
use Net::EmptyPort qw(empty_port check_port); # get a random free port my $port = empty_port(); # check if a port is already used if (check_port(5000)) { say "Port 5000 already in use"; }
Net::EmptyPort helps finding an empty TCP/UDP port.
empty_port()
empty_port(\%args)
empty_port($port)
empty_port($port, $proto)
my $port = empty_port();
Returns a port number that is NOT in use.
The function recognizes the following keys when given a hashref as the argument.
host
specifies the address on which the search should be performed. Default is 127.0.0.1.
127.0.0.1
port
Lower bound of the search for an empty port. If omitted, the function searches for an empty port within 49152..65535.
See http://www.iana.org/assignments/port-numbers
proto
Name of the protocol. Default is tcp. You can find an empty UDP port by specifying udp.
tcp
udp
To maintain backwards compatibility, the function accepts scalar arguments as well. For example, you can also find an empty UDP port by specifying the protocol as the second parameter:
my $port = empty_port(1024, 'udp'); # use 49152..65535 range my $port = empty_port(undef, 'udp');
check_port(\%args)
check_port($port)
check_port($port, $proto)
my $true_or_false = check_port(5000);
Checks if the given port is already in use. Returns true if it is in use (i.e. if the port is NOT free). Returns false if the port is free.
specifies the port to check. This argument is mandatory.
name of the protocol. Default is tcp.
To maintain backwards compatibility, the function accepts scalar arguments as well in the form described above.
wait_port(\%args)
wait_port($port)
wait_port($port, $max_wait)
wait_port($port, $max_wait, $proto)
Waits until a particular port becomes ready to connect to. Returns true if the port becomes ready, or false if otherwise.
max_wait
maximum seconds to wait for (default is 10 seconds). Pass a negative value to wait infinitely.
Incompatible changes: Before 2.0, wait_port($port:Int[, $sleep:Number, $retry:Int, $proto:String]) is a signature.
wait_port($port:Int[, $sleep:Number, $retry:Int, $proto:String])
can_bind($host)
can_bind($host, $port)
can_bind($host, $port, $proto)
Checks if the application is capable of binding to given port.
Tokuhiro Matsuno <tokuhirom@gmail.com>
kazuhooku
dragon3
charsbar
Tatsuhiko Miyagawa
lestrrat
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Test::TCP, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::TCP
CPAN shell
perl -MCPAN -e shell install Test::TCP
For more information on module installation, please visit the detailed CPAN module installation guide.