#SERVER: use strict; use warnings; use 5.010; use IO::Socket; use IO::Select; my $LISTEN_SOCK = IO::Socket::INET->new( LocalPort => 12555, Listen => 5, Reuse => 1, ) or die "Couldn't create socket: $@"; my $sock_group = IO::Select->new() or die "Couldn't create select"; $sock_group->add($LISTEN_SOCK); warn "listening for connections...\n"; while (1) { my @ready_to_read = $sock_group->can_read; for my $READABLE_SOCK (@ready_to_read) { if ($READABLE_SOCK eq $LISTEN_SOCK) { my $NEW_CONNECTION = $LISTEN_SOCK->accept() or warn "Couldn't connect: $!"; if($NEW_CONNECTION) { $sock_group->add($NEW_CONNECTION); } } else { my $status = $READABLE_SOCK->sysread( my($available_data), 8096 ); if ($status > 0) { #read some data STDOUT->syswrite($available_data); } elsif ($status == 0) { #read eof=>client closed socket $sock_group->remove($READABLE_SOCK); close $READABLE_SOCK; } else { #undef=>IO error warn "IO error while reading socket: $READABLE_SOCK"; } } } }