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

Hi Monks, I'm trying to write a network server which has to handle many concurrent connections. If I write it using accept()/fork(), i quickly wind up with too many processes, none of which gets a chance to do anything useful. It is possible that perl is not the tool for this job, but I'm having a go using using IO::Epoll. I'm currently confused about getting notification that the socket has gone away - I register EPOLLHUP in my epoll_ctl, but when I close a client connection, I only receive the read event. I know that I need to use sysread instead of readline, but it's not causing me a problem at the minute. The server code is
#!/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]); } } } } }
If I use another terminal to telnet to localhost port 12345, type in "hello\n", then close the connection, i get the output
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 = [];
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...

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
    You could save yourself the hard work of polling all the sockets manually and use POE. It handles all the IO multiplexing for you behind a high level event system using select/poll/epoll/whatever for the backend. It works especially well for network applications, and there are modules available to connect it to many common internet protocols.
Re: IO::Epoll - detecting sockets which have gone away
by Illuminatus (Curate) on Oct 23, 2007 at 20:46 UTC
    I am looking at the server code, and don't see where you look for the EPOLLHUP. I see where you check for EPOLLIN, but this is always set when epoll returns an fd with data. I have never used Epoll (I generally use select), and it could well be that it never sets EPOLLHUP. When using select, the only way to detect a socket close is when it reports a read available on the fd, but returns 0 on a read.

    As for your comment about perl not being the best tool for the job, well I won't even dignify that with a comment.