To get away from your obsession on readline,
Observation is not obsession.
I can kill the client's xterm, before I send any newline, and the server stays responsive to new and other connections.
Sure you can if you fork, but that's a different solution. Ie. a forking server not a multiplexing server.
Here is a perfectly serviceable equivalent using threads:
#! perl -sw use strict; use threads stack_size => 4096; use threads::shared; use IO::Socket; use constant CRLF => chr( 13 ) . chr( 10 ); $/ = $\ = CRLF; my $server = IO::Socket::INET->new( LocalHost => 'localhost', LocalPort => 1025, Listen => SOMAXCONN, Reuse => 1, ) or die "Couldn't create listening socket"; while( 1 ) { my $client = $server->accept; async { my $peerhost = $client->peerhost .':'. $client->peerport; while( <$client> ){ chomp; print $client $_; print "$peerhost:$_"; } }->detach; } close $server;
That will (has) run all day and night without problems.
And here is a 'one-liner' that will launch 1000 clients at it each time:
perl -Mthreads=stack_size,4096 -MIO::Socket -E" async{ my$s=new IO::Socket::INET('localhost:1025') or warn $^E, return; say $s 'Hello'; print scalar <$s>; shutdown $s, 2; close $s; }->detach for 1 .. 1e6"
But as with all forking servers, it is ridiculously resource intensive for an echo daemon.
In reply to Re^6: Does IO::Select work? Anywhere?
by BrowserUk
in thread Does IO::Select work? Anywhere?
by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |