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=>"perlmonks.org:80") 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 bugs.devel.redhat.com 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/libc-2.3.2.so, 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 .
-BronzeWing
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 | |
|
Re: $Socket->connected Not Returning False?
by ant9000 (Monk) on Oct 21, 2003 at 12:58 UTC | |
by BronzeWing (Monk) on Oct 21, 2003 at 17:55 UTC | |
by ivanatora (Sexton) on Jan 15, 2006 at 09:10 UTC | |
by tirwhan (Abbot) on Jan 15, 2006 at 09:32 UTC |