Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

$Socket->connected Not Returning False?

by BronzeWing (Monk)
on Oct 21, 2003 at 02:29 UTC ( #300813=perlquestion: print w/replies, xml ) Need Help??

BronzeWing has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks,
I fear I'm missing something obvious, but for the life of me I can't figure it out. The code below is the minimal case for what I'm trying to do. $Socket->connected doesn't return false when I'm disconnected unless I try to send further data to the other side of the connection. It just keeps reading empty strings from the socket (which, without blocking on, is exactly the same as being connected but not having data on the line). However, when I use netstat, it says that the connection is quite clearly closed. I need the while loop to exit when I'm disconnected, as is expected. What am I doing wrong?

Here's the code...

#!/usr/bin/perl use warnings; use strict; use IO::Socket::INET; my $Socket = IO::Socket::INET->new(PeerAddr=>"") or di +e; $Socket->send("GET /404 HTTP/1.0$/$/") or die; while ($Socket->connected) { $Socket->recv(my($Byte),1); if (length($Byte)) { print "Byte: $Byte$/"; } else { print "No byte.$/"; } }

Here's perl -V:

Summary of my perl5 (revision 5.0 version 8 subversion 1) configuratio +n: Platform: osname=linux, osvers=2.4.21-1.1931.2.307.entbigmem, archname=i386- +linux-thread-multi uname='linux 2.4.21-1.1931.2.307.entbigmem # +1 smp sat jul 5 11:55:51 edt 2003 i686 i686 i386 gnulinux ' config_args='-des -Doptimize=-O2 -g -pipe -march=i386 -mcpu=i686 - +Dversion=5.8.0 -Dmyhostname=localhost -Dperladmin=root@localhost -Dcc +=gcc -Dcf_by=Red Hat, Inc. -Dinstallprefix=/usr -Dprefix=/usr -Darchn +ame=i386-linux -Dvendorprefix=/usr -Dsiteprefix=/usr -Dotherlibdirs=/ +usr/lib/perl5/5.8.1 -Duseshrplib -Dusethreads -Duseithreads -Duselarg +efiles -Dd_dosuid -Dd_semctl_semun -Di_db -Ui_ndbm -Di_gdbm -Di_shado +w -Di_syslog -Dman3ext=3pm -Duseperlio -Dinstallusrbinperl -Ubincompa +t5005 -Uversiononly -Dpager=/usr/bin/less -isr' hint=recommended, useposix=true, d_sigaction=define usethreads=define use5005threads=undef useithreads=define usemulti +plicity=define useperlio=define d_sfio=undef uselargefiles=define usesocks=undef use64bitint=undef use64bitall=undef uselongdouble=undef usemymalloc=n, bincompat5005=undef Compiler: cc='gcc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS + -DDEBUGGING -fno-strict-aliasing -I/usr/local/include -D_LARGEFILE_S +OURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/gdbm', optimize='-O2 -g -pipe -march=i386 -mcpu=i686', cppflags='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -DDEBUGGI +NG -fno-strict-aliasing -I/usr/local/include -I/usr/include/gdbm' ccversion='', gccversion='3.3 20030623 (Red Hat Linux 3.3-12)', gc +cosandvers='' intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234 d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=1 +2 ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', + lseeksize=8 alignbytes=4, prototype=define Linker and Libraries: ld='gcc', ldflags =' -L/usr/local/lib' libpth=/usr/local/lib /lib /usr/lib libs=-lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lpthread -lc perllibs=-lnsl -ldl -lm -lcrypt -lutil -lpthread -lc libc=/lib/, so=so, useshrplib=true, libperl=libperl.s +o gnulibc_version='2.3.2' Dynamic Linking: dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-rdynami +c -Wl,-rpath,/usr/lib/perl5/5.8.1/i386-linux-thread-multi/CORE' cccdlflags='-fPIC', lddlflags='-shared -L/usr/local/lib' Characteristics of this binary (from libperl): Compile-time options: DEBUGGING MULTIPLICITY USE_ITHREADS USE_LARGE_ +FILES PERL_IMPLICIT_CONTEXT Locally applied patches: RC2 Built under linux Compiled at Jul 11 2003 23:02:08 @INC: /usr/lib/perl5/5.8.1/i386-linux-thread-multi /usr/lib/perl5/5.8.1 /usr/lib/perl5/site_perl/5.8.1/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.1 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.1/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.1 /usr/lib/perl5/vendor_perl /usr/lib/perl5/5.8.1/i386-linux-thread-multi /usr/lib/perl5/5.8.1 .


You are in a maze of twisty little knowledge base documents, all alike.
It is dark. You are likely to be eaten by a lawyer.

Replies are listed 'Best First'.
Re: $Socket->connected Not Returning False?
by pg (Canon) on Oct 21, 2003 at 04:29 UTC

    connected can not be used to check the actual state of the connection, instead it tells you whether the socket is locally closed.

    What you can do is to use IO::Select, call can_read first, if can_read says yes, but when you read, you got undef back, then the connection is actually closed.

    This solution resolves the confusion you mentioned, that you could not differentiate whether the socket is closed or just got an empty string on a open socket.

    The following code demos this (tested under win98 with AS 5.8.0 build 802): use IO::Socket; use strict; my $server = new IO::Socket::INET(Timeout => 7200, Proto => "tcp", LocalPort => 3000, LocalHost => "localhost", Reuse => 1, Listen => 2); print "Server is listening for connection ...\n"; my $c = $server->accept; print "Connection accepted, now sleep 10 seconds ...\n"; sleep(10); close $c; print "Connection closed\n"; use IO::Socket; use IO::Select; use strict; my $c = new IO::Socket::INET(Proto => "tcp", PeerAddr => "localhost", PeerPort => 3000, Timeout => 7200) || die "failed to connect to server\n"; print "Connected, $c\n"; my $sel = new IO::Select($c); while (1) { print "tick ...\n"; my @r = $sel->can_read(0); foreach (@r) { my $line = <$_>; print "$_ is closed\n" if (!defined($line)); } sleep(1); }
Re: $Socket->connected Not Returning False?
by ant9000 (Monk) on Oct 21, 2003 at 12:58 UTC
    You could use sysread like this:
    use IO::Socket::INET; my $Socket = IO::Socket::INET->new( PeerAddr=>"" ) or die; $Socket->send("GET /404 HTTP/1.0$/$/") or die; my $still_connected=1; while($still_connected){ my $Byte; my $read=sysread($Socket,$Byte,1); if(defined($read)){ if($read==0){ $still_connected=0; }else{ print "Byte: '$Byte'$/"; } }else{ if($!==EAGAIN()){ print "No byte.$/"; }else{ print "Error $!.$/"; $still_connected=0; } } }


      Thank you! I've added a case to my original code that watches for !defined($read) and $! != EAGAIN(), and closes the socket locally, allowing $Socket->connected to return false. It works perfectly now.


      You are in a maze of twisty little knowledge base documents, all alike.
      It is dark. You are likely to be eaten by a lawyer.

      What exactly is EAGAIN() ?

        man 2 read. You tried a non-blocking I/O operation which failed because no data was there to be read, and you should try again.

        There are ten types of people: those that understand binary and those that don't.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://300813]
Approved by pg
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2023-03-30 17:21 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (74 votes). Check out past polls.