You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi all,
I want to listen on dynamically selected port using IO::Socket::INET.
When I specify LocalAddr to '127.0.0.1' or so, it works.
However it doesn't work for '0.0.0.0'.
The code in IO/Socket/INET.pm seems intentionally prohibit this.
I'm just curious to know the reason.
N.A.
--- works well --
use IO::Socket;
my $sock = IO::Socket::INET->new( Proto => 'udp', LocalPort => 0,
LocalAddr => '127.0.0.1' );
say $sock->sockport();
------
--- doesn't work --
use IO::Socket;
my $sock = IO::Socket::INET->new( Proto => 'udp', LocalPort => 0,
LocalAddr => '0.0.0.0' );
say $sock->sockport();
------
--- IO/Socket/INET.pm line 202 --
if($lport || ($laddr ne INADDR_ANY) || exists $arg->{Listen}) {
$sock->bind($lport || 0, $laddr) or
return _error($sock, $!, "$!");
}
-------
The text was updated successfully, but these errors were encountered:
if($lport || ($laddr ne INADDR_ANY) || exists $arg->{Listen}) {
$sock->bind($lport || 0, $laddr) or
return _error($sock, $!, "$!");
}
Then if exists($arg->{PeerAddr}) is true, it will try to make an outgoing connect() to a remote socket.
The ($laddr ne INADDR_ANY) check seems to be there to stop the module from doing both a bind() and a connect() call, when only a connect() is appropriate.
IO::Socket::INET will do what the OP wants if the Listen argument is also passed to the constructor.
IO::Socket::IP is preferred nowadays and seems to do what the OP wants out of the box:
use v5.40;
use IO::Socket::IP;
my $sock = IO::Socket::IP->new( Proto => 'udp', LocalPort => 0, LocalAddr => '0.0.0.0' );
say $sock->sockhost();
say $sock->sockport();
Migrated from rt.cpan.org#91231 (status was 'new')
Requestors:
From [email protected] on 2013-12-07 02:19:52:
The text was updated successfully, but these errors were encountered: