Strange problem with IO::Socket::connected()

M

Manni Heumann

Hi,

I'm currently working on a program that will connect to a server to retrieve
data. When connecting to the server, I check whether the return value of
connected is defined, otherwise an error is raised. Here's some code:

my $socket = IO::Socket::INET->new(
PeerAddr => '127.0.0.1',
PeerPort => 5555,
Type => SOCK_STREAM,
Proto => 'tcp',
Timeout => 1,
);

if ( defined $socket && $socket->connected() ) {
....

While working with a mock-up version of the server (running on localhost),
this works just fine and just like I think it's supposed to work. But when
working with the real server (which I have no control over), the call to
connected() returns undef, although the socket clearly is connected and I can
use it to communicate with the server.

How can connected return undef when the socket really is connected? If that
is the correct behavior and not some bug in the underlying system call, how
can I check whether I really am connected?



TIA,
Manni
 
B

Bart Van der Donck

Manni said:
I'm currently working on a program that will connect to a server to retrieve
data. When connecting to the server, I check whether the return value of
connected is defined, otherwise an error is raised. Here's some code:

my $socket = IO::Socket::INET->new(
PeerAddr => '127.0.0.1',
PeerPort => 5555,
Type => SOCK_STREAM,
Proto => 'tcp',
Timeout => 1,
);

if ( defined $socket && $socket->connected() ) {
...

While working with a mock-up version of the server (running on localhost),
this works just fine and just like I think it's supposed to work. But when
working with the real server (which I have no control over), the call to
connected() returns undef, although the socket clearly is connected and I can
use it to communicate with the server.

How can connected return undef when the socket really is connected? If that
is the correct behavior and not some bug in the underlying system call, how
can I check whether I really am connected?

You can trust that you're connected if $socket is not undef. No need to
perform an additional check on $socket->connected for that. You should
stay connected until EOF, or unless you ' close ($socket); '.

die "Could not create socket: $!\n" unless $socket;

should be okay.

connected() is a relatively new method of IO::Socket. Possibly it is
not working 100% depending on network configuration / proxy's /
firewall / buffering methods ... I was not able to reconstruct your
situation on WinXP and FreeBSD UNIX though (both having
IO::Socket::INET 1.27).

Hope this can be of some help,
 
T

Thomas Kratz

Manni said:
Hi,

I'm currently working on a program that will connect to a server to retrieve
data. When connecting to the server, I check whether the return value of
connected is defined, otherwise an error is raised. Here's some code:

my $socket = IO::Socket::INET->new(
PeerAddr => '127.0.0.1',
PeerPort => 5555,
Type => SOCK_STREAM,
Proto => 'tcp',
Timeout => 1,
);

if ( defined $socket && $socket->connected() ) {
...

A look into the IO::Socket module reveals that the connected method is
only a disguised call to getpeername($socket) (see perldoc -f getpeername).

IIRC it will only return something useful if you have established a
connection with either $socket->connect or $socket->accept.
I don't think it makes sense immediatly after IO::Socket::INET->new.

There are a number of reasons getpeername can fail. You have to look at
the getpeername documentation for your OS.

Thomas

--
$/=$,,$_=<DATA>,s,(.*),$1,see;__END__
s,^(.*\043),,mg,@_=map{[split'']}split;{#>J~.>_an~>>e~......>r~
$_=$_[$%][$"];y,<~>^,-++-,?{$/=--$|?'"':#..u.t.^.o.P.r.>ha~.e..
'%',s,(.),\$$/$1=1,,$;=$_}:/\w/?{y,_, ,,#..>s^~ht<._..._..c....
print}:y,.,,||last,,,,,,$_=$;;eval,redo}#.....>.e.r^.>l^..>k^.-
 
M

Manni Heumann

Bart said:
Manni Heumann wrote:

You can trust that you're connected if $socket is not undef. No need to
perform an additional check on $socket->connected for that. You should
stay connected until EOF, or unless you ' close ($socket); '.

die "Could not create socket: $!\n" unless $socket;

should be okay.

I hope you're right. I vaguely remember that I had a case where new would
return a valid object, that could not be used for communication and where
connected returned undef. Maybe that was a case where the server was
listening on that port and didn't call accept(), but I'm not sure about that.
connected() is a relatively new method of IO::Socket. Possibly it is
not working 100% depending on network configuration / proxy's /
firewall / buffering methods ... I was not able to reconstruct your
situation on WinXP and FreeBSD UNIX though (both having
IO::Socket::INET 1.27).

This seems to be easy to replicate on gentoo boxes, but I haven't done much
testing yet.


Thanks,

Manni
 
M

Manni Heumann

Thomas said:
A look into the IO::Socket module reveals that the connected method is
only a disguised call to getpeername($socket) (see perldoc -f
getpeername).

Yup. I should have mentioned that in my original post since it's something
that I find very puzzling. How can the system have a connection without
knowing where it's connected to?
IIRC it will only return something useful if you have established a
connection with either $socket->connect or $socket->accept.
I don't think it makes sense immediatly after IO::Socket::INET->new.

Not quite. If you provide enough information to IO::Socket::INET->new, then
new() will call connect().
There are a number of reasons getpeername can fail. You have to look at
the getpeername documentation for your OS.

I did check the man page, but apart from reasons like system overload, the
only reason for getpeername to fail given was that the socket is not
connected. Smells like a bug in the getpeername system call or the
interaction of that system call with the Perl internals.


Thanks,

Manni
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top