The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Net::EmptyPort - find a free TCP/UDP port

SYNOPSIS

    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";
    }

DESCRIPTION

Net::EmptyPort helps finding an empty TCP/UDP port.

METHODS

empty_port()
    my $port = empty_port();

Get the available port number, you can use.

Normally, empty_port() finds empty port number from 49152..65535. See http://www.iana.org/assignments/port-numbers

But you want to use another range, use a following form:

    # 5963..65535
    my $port = empty_port(5963);

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($port:Int)
    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.

Also works for UDP:

    my $true_or_false = check_port(5000, 'udp');
wait_port($port:Int[, $max_wait:Number,$proto:String])

Waits for a particular port is available for connect.

This method waits the $port number is ready to accept a request.

$port is a port number to check.

Sleep up to $max_wait seconds for checking the port.

Return value : Return true if the port is available, false otherwise.

Incompatible changes: Before 2.0, wait_port($port:Int[, $sleep:Number, $retry:Int, $proto:String]) is a signature.

AUTHOR

Tokuhiro Matsuno <tokuhirom@gmail.com>

THANKS TO

kazuhooku

dragon3

charsbar

Tatsuhiko Miyagawa

lestrrat

SEE ALSO

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.