Net::EmptyPort - find a free TCP/UDP port
use Net::EmptyPort qw(empty_port check_port); # get a socket listening on a random free port my $socket = listen_socket(); # 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.
listen_socket()
listen_socket(\%args)
my $socket = listen_socket();
Returns a socket listening on a free port.
The function recognizes the following keys in the hashref argument.
host
The address on which to listen. Default is 127.0.0.1.
127.0.0.1
proto
Name of the protocol. Default is tcp. You can get an UDP socket by specifying udp.
tcp
udp
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.
specifies the address on which the search should be performed. Default is 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
Name of the protocol. Default is tcp. You can find an empty UDP port by specifying 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.
When UDP is specified as the protocol, the `check_port` function sends a probe UDP packet to the designated port to see if an ICMP error message is returned, which indicates that the port is unassigned. The port is assumed to be assigned, unless such response is observed within 0.1 seconds.
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.