use IO::Select; use IO::Socket; use POSIX; ##### setup the file.... # open it open my $tail, $FILE or die $!; # set to non-blocking fcntl( $tail, F_SETFL(), O_NONBLOCK() ); # [ fill up the file info buffer ] ###### setup the socket.... $main_sock; $readable_handles; $main_sock = new IO::Socket::INET( LocalHost => 'localhost', LocalPort => $PORT, Listen => 8, Proto => 'tcp', ReuseAddr => 1, ); $readable_handles = new IO::Select(); $readable_handles->add($main_sock); while (1) { ######### do file tail stuff here... # unset the EOF (as seen in Perl Cookbook) $tail->clearerr(); # read any new lines if ( @lines = (<$tail>) ) { # [ add @lines to buffer ] # [ discard old lines from buffer ] # [ other processing of data... ] } ######### do TCP socket stuff.... ($new_readable) = IO::Select->select( $readable_handles, undef, undef, $TIMEOUT ); foreach my $sock (@$new_readable) { if ( $sock == $main_sock ) { $new_sock = $sock->accept(); $new_sock->autoflush(1); $readable_handles->add($new_sock); } else { if ( $ret = sysread $sock, $buf, 4 ) { # [ process the received info, generate $reply ] # reply to the client print $sock $reply; } # close the client $readable_handles->remove($sock); close $sock; } } }