"erskine, michael" wrote:
>
> When creating a new listening socket filehandle with IO::Socket::INET->new()
> I can't seem to detect when it fails at the automatic bind or listen stage;
> e.g.: -
>
> #! perl -w
> use strict; use IO::Socket; use IO::Select;
>
> my $l1 = IO::Socket::INET->new(
> Proto => 'tcp',
> LocalPort => 2070,
> Listen => SOMAXCONN,
> Reuse => 1);
>
> print STDOUT "After \$l1, \$l1->error() = '", $l1->error(), "' and \$! =
> '$!'.\n";
>
> # attempt to listen on same port...
>
> my $l2 = IO::Socket::INET->new(
> Proto => 'tcp',
> LocalPort => 2070,
> Listen => SOMAXCONN,
> Reuse => 1);
>
> print STDOUT "After \$l2, \$l2->error() = '", $l2->error(), "' and \$! =
> '$!'.\n";
>
> __END__
>
> ...this will result in...
>
> After $l1, $l1->error() = '0' and $! = ''.
> After $l2, $l2->error() = '0' and $! = ''.
>
> ...when, clearly, both sockets cannot bind to the same local port! What
> technique is available to establish whether the automatic bind or listen
> failed?
I ran this test:
use strict;
use IO::Socket;
use IO::Select;
use Data::Dumper; $Data::Dumper::Indent=1;
my $l1 = IO::Socket::INET->new(Proto => 'tcp', LocalPort => 2070,
Listen => SOMAXCONN, Reuse => 1) or warn "Error on Listen 1: $!\n";
print &Data::Dumper::Dumper($l1);
# attempt to listen on same port...
my $l2 = IO::Socket::INET->new(Proto => 'tcp', LocalPort => 2070,
Listen => SOMAXCONN, Reuse => 1) or warn "Error on Listen 2: $!\n";
print &Data::Dumper::Dumper($l2);
my $readable = IO::Select->new;
$readable->add($l1, $l2);
while (1) {
# Get a list of sockets that are ready to talk to us.
my ($ready) = IO::Select->select($readable, undef, undef, undef);
foreach my $s (@$ready) {
# Accept the connection and add it to our readable list.
print &Data::Dumper::Dumper($s);
if ($s == $l1) {
my $new_sock = $s->accept;
if ($new_sock) {
$readable->add($new_sock);
print $new_sock "Welcome 1!\r\n";
}
print STDERR "New l1 connection accepted\n";
} elsif ($s == $l2) {
my $new_sock = $s->accept;
if ($new_sock) {
$readable->add($new_sock);
print $new_sock "Welcome 2!\r\n";
}
print STDERR "New l2 connection accepted\n";
} else {
print STDERR "Data:\n";
$_ = <$s>;
print STDERR $_;
}
}
}
__END__
and these commandline results (using telnet to connect several times):
First run:
$VAR1 = bless( \*Symbol::GEN0, 'IO::Socket::INET' );
$VAR1 = bless( \*Symbol::GEN1, 'IO::Socket::INET' );
$VAR1 = bless( \*Symbol::GEN1, 'IO::Socket::INET' );
New l2 connection accepted
$VAR1 = bless( \*Symbol::GEN1, 'IO::Socket::INET' );
New l2 connection accepted
$VAR1 = bless( \*Symbol::GEN1, 'IO::Socket::INET' );
New l2 connection accepted
Second run:
$VAR1 = bless( \*Symbol::GEN0, 'IO::Socket::INET' );
$VAR1 = bless( \*Symbol::GEN1, 'IO::Socket::INET' );
$VAR1 = bless( \*Symbol::GEN0, 'IO::Socket::INET' );
New l1 connection accepted
$VAR1 = bless( \*Symbol::GEN0, 'IO::Socket::INET' );
New l1 connection accepted
Notice that the second set used the first socket instead of the second.
If you turn off Reuse on the second socket, it will produce an error
as you would suspect.
Do a netstat -a after it is running:
> netstat -a
Active Connections
Proto Local Address Foreign Address State
TCP dollar:2070 DOLLAR:0 LISTENING
TCP dollar:2070 DOLLAR:0 LISTENING
it shows there are actually two listens there. I guess it's possible
to have two listens on the same port since there is no connection and
a connection is defined as socketpair and each socket has an address
family, protocol, IP addr and port and there is no real connection in
this case until you do an accept.
You can certainly have multiple sockets (from different remote addrs/ports)
connected to the same listen port. I guess there is no way to determine
which socket will get the connects, but preliminary results favor the
same socket getting all of the connects.
--
,-/- __ _ _ $Bill Luebkert ICQ=14439852
(_/ / ) // // DBE Collectibles http://www.wgn.net/~dbe/
/ ) /--< o // // Mailto:[EMAIL PROTECTED] http://dbecoll.webjump.com/
-/-' /___/_<_</_</_ http://www.freeyellow.com/members/dbecoll/
_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users