RenalPete has asked for the wisdom of the Perl Monks concerning the following question:
If I use another terminal to telnet to localhost port 12345, type in "hello\n", then close the connection, i get the output#!/usr/bin/perl -w use strict; use IO::Socket; use IO::Epoll;# qw(:compat); use Data::Dumper; my $server = IO::Socket::INET->new(LocalPort => 12345, Type => SOCK_ST +REAM, Listen => 1) or die $!; my $poll = epoll_create(10); epoll_ctl($poll, EPOLL_CTL_ADD, fileno $server, EPOLLIN | EPOLLERR | E +POLLHUP) >= 0 || die "epoll_ctl: $!\n"; print "fd ".fileno($server)." EPOLLIN ".EPOLLIN." EPOLLERR ".EPOLLERR. +" EPOLLHUP ".EPOLLHUP."\n"; my @client; while(1){ if(my $events = epoll_wait($poll, 10,1000)){ print "epoll said: ".Data::Dumper->Dump([$events]); for my $e (@$events){ if ($e->[0] == fileno($server)){ #new connection my $client = $server->accept(); $client[fileno($client)] = $client; epoll_ctl($poll, EPOLL_CTL_ADD, fileno + $client, EPOLLIN | EPOLLERR |EPOLLHUP) >= 0 || die "epoll_ctl clien +t: $!\n"; }else{ #existing connection if ($e->[1] & EPOLLIN){ my $x = readline($client[$e->[ +0]]); print "client ".$e->[0]." said + '$x'\n"; if (!defined($x)){ close $client[$e->[0]] +; } }else{ warn "polled\n"; print "unexpected event: ".Dat +a::Dumper->Dump([$e]); } } } } }
fileno 3 is the server socket, 5 is the client I accept()ed. epoll_wait returns a listref of listrefs, which are [fd, EVENT1 | EVENT2] - it reads the line "hello\n" from the client OK, then when I close the client it generates another read event ([5,1]), and I have a hack to detect the socket has gone. Do you know how to do it properly? Thanks in advance...fd 3 EPOLLIN 1 EPOLLERR 8 EPOLLHUP 16 epoll said: $VAR1 = []; epoll said: $VAR1 = []; epoll said: $VAR1 = [ [ '3', '1' ] ]; epoll said: $VAR1 = []; epoll said: $VAR1 = [ [ '5', '1' ] ]; client 5 said 'hello ' epoll said: $VAR1 = []; epoll said: $VAR1 = [ [ '5', '1' ] ]; Use of uninitialized value in concatenation (.) or string at epoll.pl +line 27, <GEN1> line 1. client 5 said '' epoll said: $VAR1 = []; epoll said: $VAR1 = [];
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: IO::Epoll - detecting sockets which have gone away
by ailivac (Sexton) on Oct 24, 2007 at 00:52 UTC | |
|
Re: IO::Epoll - detecting sockets which have gone away
by Illuminatus (Curate) on Oct 23, 2007 at 20:46 UTC |