Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Strange problem with IO::Socket::connected()

Reply
Thread Tools

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

 
 
Manni Heumann
Guest
Posts: n/a
 
      03-28-2006
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
 
Reply With Quote
 
 
 
 
Bart Van der Donck
Guest
Posts: n/a
 
      03-28-2006
Manni Heumann wrote:

> 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,

--
Bart

 
Reply With Quote
 
 
 
 
Dr.Ruud
Guest
Posts: n/a
 
      03-28-2006
Bart Van der Donck schreef:

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


Alternative:

$socket or die "Could not create socket: $!";

--
Affijn, Ruud

"Gewoon is een tijger."
echo 014C8A26C5DB87DBE85A93DBF |perl -pe 'tr/0-9A-F/JunkshoP cartel,/'
 
Reply With Quote
 
Thomas Kratz
Guest
Posts: n/a
 
      03-28-2006
Manni Heumann wrote:
> 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^.-
 
Reply With Quote
 
Manni Heumann
Guest
Posts: n/a
 
      03-29-2006
Bart Van der Donck wrote:

> Manni Heumann wrote:


>> 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.


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
 
Reply With Quote
 
Manni Heumann
Guest
Posts: n/a
 
      03-29-2006
Thomas Kratz wrote:

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
AVG Email Scanner activating at strange times with strange IP addresses dennispublic@hotmail.com Computer Support 1 08-26-2006 04:27 AM
strange problem John Wireless Networking 4 02-14-2005 05:40 PM
Problem installing modules on redhat 9 - strange connection problem championsleeper Perl Misc 0 01-26-2005 12:19 PM
Strange error! Compiler problem? Or code problem?!!!!!!! Pedro Miguel Carvalho C++ 2 10-25-2004 02:06 PM
Question About Strange 'C' Code Syntax ( Well strange to me anyway ) Harvey Twyman C Programming 8 10-25-2003 05:54 AM



Advertisments