use Socket; use Fcntl; use IO::Socket; use IO::Select; my $serv = IO::Socket::INET->new(LocalPort => 9001, Reuse => 1, Listen => 10) or die "Cannot open server: $!\n"; nonblock($serv); my $sel = IO::Select->new($serv); pipe $MYSTDERR, $MYWRITE; *STDERR = $MYWRITE; $sel->add($MYSTDERR); while(1){ for my $sock ($sel->can_read(0)) { if($sock == $MYSTDERR){ my $line = <$sock>; print $line; for my $s ($sel->handles()){ if($s != $MYSTDERR and $s != $serv){ print $s $line; } } } elsif($sock == $serv){ my $new = $serv->accept(); nonblock($new); $sel->add($new); bcast($new, "New Connection"); } else { # this is a client reading my $line = <$sock>; if($line){ chomp($line); bcast($sock, "Data: $line"); } else { bcast($sock, "Connection Dropped"); $sel->remove($sock); } } } } sub bcast{ my $sock = shift; warn sprintf "[%s:%s] @_\n", $sock->peerhost, $sock->peerport; } sub nonblock { my $socket = shift; my $flags; $flags = fcntl($socket, F_GETFL, 0) or die "Can't get flags for socket: $!\n"; fcntl($socket, F_SETFL, $flags | O_NONBLOCK) or die "Can't make socket nonblocking: $!\n"; }