my $w = EV::io $client, EV::READ, \&handle_client; #### use strict; use IO::Socket; use EV; my $connection_count = 0; my $h; my %h; my $server = new IO::Socket::INET ( LocalPort => 2345, Type => SOCK_STREAM, Listen => SOMAXCONN, Reuse => 1 ); my $w = EV::io $server, EV::READ, \&handle_incoming; EV::loop; sub handle_incoming { my $w=shift; my $fh=$w->fh->accept or die; $h->{$fh} = EV::io $fh, EV::READ, \&handle_client; printf "new socket connection #%d (%d)\n", ++$connection_count, scalar keys %$h; } sub handle_client { my $c=shift; my $fh=$c->fh; my $bytes_read=sysread($fh, my $bytes, 9_999_999); return if (not exists $h->{$fh}); printf "in=%s (%d bytes)\n",$bytes,$bytes_read; if (($bytes eq 'q') || ($bytes_read == 0)) { close($fh); undef $c; # destroy event watcher delete $h->{$fh}; printf "socket connection terminated by %s (%d (%d) sockets remaining)\n", $bytes_read == 0 ? 'peer' : 'server', --$connection_count, scalar keys %$h; } }