Here's a short example of a tcp server that handles multiple connections and detects when they are closed. No warranties, I skipped some error checking to keep it concise. Hope it helps.
#!/usr/bin/perl -w use strict; use IO::Select; use IO::Socket; my $s = IO::Select->new(); my $listener=IO::Socket::INET->new(Listen => 5, LocalPort => 9999, Proto => 'tcp'); $s->add($listener); my $buf; while (1) { # loop thru readable sockets for ($s->can_read()) { # readable event on a listener means we have a new # socket connection if ($_ == $listener) { # accept the connection and add to the select # object print "Connection...groovy\n"; my $newsock=$listener->accept; $s->add($newsock); } else { # this is a "regular" socket, not the listener # try and read up to 512 bytes from it my $return=$_->sysread($buf,512); if (!defined($return)) { # undef from sysread means an error print "error: $!\n"; $s->remove($_); $_->close; } elsif ($return == 0) { # 0 from sysread means the connection was closed print "socket was closed\n"; $s->remove($_); $_->close; } else { # a positive int from sysread means # we got some data # change non-printables to a . char $buf =~tr/\0-\37\177-377/./; print "read $return bytes from a socket [$buf]\n"; } } } }
In reply to Re: Knowing an IO::Socket handle has
by kschwab
in thread Knowing an IO::Socket handle has reached end-of-file
by dash2
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |