Hello,

Has anyone used IO::Poll (or IO::Select) to detect when a networking
socket has received a broken PIPE on the remote end, a hangup, or an error?
I am trying to have my code check for a closed down socket on the remote
end, and then reconnect.  I would appreciate any feedback, as I am very
stuck right now.

The program is a multiplexed TCP client that connects to the server
via $socket, and reads all data from the socket, writes every once in a
while, and outputs the read data to a logfile.  The particular question
is:  How do I use IO::Poll to detect a POLLHUP or POLLERR event, and
reconnect to the server?  Or how would this be done in IO::Select?

My code is almost there with IO::Poll.  I have been reading the
Lincoln Stein "Network Programming with Perl" book, and can't find a
practical way to implement this.  This excerpt from the book touches
on what I need to implement:

"On some, if not all, Linux systems, POLLIN is not set when a socket
is closed.  Instead you must check for a POLLHPUP event.  However,
POLLHUP is relevant only to sockets and pipes, and does not apply to
ordinary filehandles; this makes program logic a bit convoluted.

The most reasonable strategy is to recover the handles that may be
readable by calling handles with a bitmask POLLIN | POLLHUP | POLLERR.
Pass each handle to sysread(), and let the return value tell you what
state the handle was in.

Similarly, it is easiest to check for handles that are writable using
the bitmask POLLOUT | POLLERR.  The subsequent call to syswrite()
will indicate whether the handle is open or has an error."

How do I differentiate between POLLERR versus POLLHUP on
the return value of sysread(), syswrite()?

Or for that matter, how do
I distinguish between POLLIN and POLLERR/POLLHUP?

Here is my code:
### By this time the socket has already been created
use IO::Poll 0.04 qw (POLLIN POLLOUT POLLERR POLLHUP);


###
# Loop for infinity while VCO Monitor Reports
###
my $poll = IO::Poll->new;
$poll->mask($socket => POLLIN|POLLOUT);

while (1) {

        $poll->poll();

        my @readers = $poll->handles(POLLIN|POLLHUP|POLLERR);
        my @writers = $poll->handles(POLLOUT);

        foreach (@readers) {

                if(sysread ($socket, $buffer, 1) > 0) {

                        vcoreadsocket();

                        processmessage();

        }

        foreach (@writers) {

                check_send();

                if ($need_to_send_83 eq "yes") {
                        send83();
                }

                if ($need_to_send_82 eq "yes") {
                        send82();
                }

                if ($need_to_send_81 eq "yes") {
                        send81();
                }

        }

}


Any feedback or help will be greatly appreciated.
Thanks,
-Jason

_______________________________________________
Perl-Unix-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to