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...

In reply to IO::Epoll - detecting sockets which have gone away by RenalPete

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.