Checking whether a socket has been closed by the peer

Discussion in 'Perl Misc' started by Jakob Lell, Apr 15, 2004.

  1. Jakob Lell

    Jakob Lell Guest

    Hello,
    how can I check whether the client has closed an IO::Socket::INET?
    According to the description, the connected-Method of IO::Socket should
    return undef if the socket isn't connected. However, this doesn't work
    when the peer has closed the connection. When you run the script below
    and connect to port 1234 from another terminal using netcat or telnet and
    then close the connection, the script will continue to write "connected\n"
    to STDOUT every second.

    use IO::Socket;
    my $sock=IO::Socket::INET->new(LocalPort=>1234,Listen=>5,ReuseAddr=>1)||die $!;
    while(my $client=$sock->accept()){
    next unless defined($client);
    $client->blocking(0);
    while(defined $client->connected){
    print "connected\n";
    sleep(1);
    }
    }

    Is there any way to check whether the connection has been closed by the
    peer? I've already tried eof and the has_exception method from
    IO::Select, but neither solved my problem. I know I could try to read
    anything from the socket but as I'm using nonblocking IO, this is no
    solution for the problem.

    I'm using Linux 2.6.5 and Perl 5.8.3.

    Regards
    Jakob
     
    Jakob Lell, Apr 15, 2004
    #1
    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. Dave Rudolf
    Replies:
    3
    Views:
    11,304
    Esmond Pitt
    Dec 31, 2003
  2. John O'Conner
    Replies:
    1
    Views:
    497
    totojepast
    Jan 30, 2004
  3. Sudheer
    Replies:
    2
    Views:
    1,370
    Roedy Green
    Apr 1, 2004
  4. Guy Noir

    Re: peer to peer messaging

    Guy Noir, May 26, 2005, in forum: Java
    Replies:
    7
    Views:
    716
    Owen Jacobson
    May 31, 2005
  5. Matt Gessner
    Replies:
    1
    Views:
    1,425
Loading...

Share This Page