1Net::EmptyPort(3) User Contributed Perl Documentation Net::EmptyPort(3)
2
3
4
6 Net::EmptyPort - find a free TCP/UDP port
7
9 use Net::EmptyPort qw(empty_port check_port);
10
11 # get a socket listening on a random free port
12 my $socket = listen_socket();
13
14 # get a random free port
15 my $port = empty_port();
16
17 # check if a port is already used
18 if (check_port(5000)) {
19 say "Port 5000 already in use";
20 }
21
23 Net::EmptyPort helps finding an empty TCP/UDP port.
24
26 "listen_socket()"
27 "listen_socket(\%args)"
28 my $socket = listen_socket();
29
30 Returns a socket listening on a free port.
31
32 The function recognizes the following keys in the hashref argument.
33
34 "host"
35 The address on which to listen. Default is 127.0.0.1.
36
37 "proto"
38 Name of the protocol. Default is "tcp". You can get an UDP
39 socket by specifying "udp".
40
41 "empty_port()"
42 "empty_port(\%args)"
43 "empty_port($port)"
44 "empty_port($port, $proto)"
45 my $port = empty_port();
46
47 Returns a port number that is NOT in use.
48
49 The function recognizes the following keys when given a hashref as
50 the argument.
51
52 "host"
53 specifies the address on which the search should be performed.
54 Default is 127.0.0.1.
55
56 "port"
57 Lower bound of the search for an empty port. If omitted, the
58 function searches for an empty port within 49152..65535.
59
60 See <http://www.iana.org/assignments/port-numbers>
61
62 "proto"
63 Name of the protocol. Default is "tcp". You can find an empty
64 UDP port by specifying "udp".
65
66 To maintain backwards compatibility, the function accepts scalar
67 arguments as well. For example, you can also find an empty UDP
68 port by specifying the protocol as the second parameter:
69
70 my $port = empty_port(1024, 'udp');
71 # use 49152..65535 range
72 my $port = empty_port(undef, 'udp');
73
74 "check_port(\%args)"
75 "check_port($port)"
76 "check_port($port, $proto)"
77 my $true_or_false = check_port(5000);
78
79 Checks if the given port is already in use. Returns true if it is
80 in use (i.e. if the port is NOT free). Returns false if the port is
81 free.
82
83 The function recognizes the following keys when given a hashref as
84 the argument.
85
86 "host"
87 specifies the address on which the search should be performed.
88 Default is 127.0.0.1.
89
90 "port"
91 specifies the port to check. This argument is mandatory.
92
93 "proto"
94 name of the protocol. Default is "tcp".
95
96 To maintain backwards compatibility, the function accepts scalar
97 arguments as well in the form described above.
98
99 "wait_port(\%args)"
100 "wait_port($port)"
101 "wait_port($port, $max_wait)"
102 "wait_port($port, $max_wait, $proto)"
103 Waits until a particular port becomes ready to connect to. Returns
104 true if the port becomes ready, or false if otherwise.
105
106 The function recognizes the following keys when given a hashref as
107 the argument.
108
109 "host"
110 specifies the address on which the search should be performed.
111 Default is 127.0.0.1.
112
113 "port"
114 specifies the port to check. This argument is mandatory.
115
116 "max_wait"
117 maximum seconds to wait for (default is 10 seconds). Pass a
118 negative value to wait infinitely.
119
120 "proto"
121 name of the protocol. Default is "tcp".
122
123 To maintain backwards compatibility, the function accepts scalar
124 arguments as well in the form described above.
125
126 Incompatible changes: Before 2.0, "wait_port($port:Int[,
127 $sleep:Number, $retry:Int, $proto:String])" is a signature.
128
129 "can_bind($host)"
130 "can_bind($host, $port)"
131 "can_bind($host, $port, $proto)"
132 Checks if the application is capable of binding to given port.
133
135 Tokuhiro Matsuno <tokuhirom@gmail.com>
136
138 kazuhooku
139
140 dragon3
141
142 charsbar
143
144 Tatsuhiko Miyagawa
145
146 lestrrat
147
150 This library is free software; you can redistribute it and/or modify it
151 under the same terms as Perl itself.
152
153
154
155perl v5.28.0 2017-05-11 Net::EmptyPort(3)