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

Discussion in 'Perl Misc' started by Manni Heumann, Mar 28, 2006.

  1. 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
     
    Manni Heumann, Mar 28, 2006
    #1
    1. Advertising

  2. 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
     
    Bart Van der Donck, Mar 28, 2006
    #2
    1. Advertising

  3. Manni Heumann

    Dr.Ruud Guest

    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,/'
     
    Dr.Ruud, Mar 28, 2006
    #3
  4. Manni Heumann

    Thomas Kratz Guest

    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^.-
     
    Thomas Kratz, Mar 28, 2006
    #4
  5. 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
     
    Manni Heumann, Mar 29, 2006
    #5
  6. 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
     
    Manni Heumann, Mar 29, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. manuel aldana

    socket connected??

    manuel aldana, May 4, 2004, in forum: Java
    Replies:
    10
    Views:
    12,672
    Michael Borgwardt
    May 6, 2004
  2. My Name
    Replies:
    9
    Views:
    10,442
    Roedy Green
    Jul 21, 2004
  3. Replies:
    5
    Views:
    478
  4. Replies:
    18
    Views:
    665
    Steve Holden
    Jul 20, 2006
  5. Gordon Beaton
    Replies:
    23
    Views:
    2,002
    Roedy Green
    Sep 23, 2007
Loading...

Share This Page