#!/usr/bin/perl use IPC::Open2; use IO::Socket; my $server = IO::Socket::INET->new( LocalPort => 8000, Listen => 1, Reuse => 1, Proto => 'tcp' ) or die $@; my $client = $server->accept; my ($output, $input); my $pid = open2($output, $input, '/usr/bin/bc'); my $length = 80; my $readable; vec($readable, fileno($output), 1) = 1; vec($readable, fileno($client), 1) = 1; while (select($readable, undef, undef, undef)) { if (vec($readable, fileno($output), 1)) { my $data; sysread $output, $data, $length; # Read output from program syswrite $client, $data; # Forward to client print 'PROGRAM:', "\t", $data; } if (vec($readable, fileno($client), 1)) { my $data; sysread $client, $data, $length; # Read input from client syswrite $input, $data; # Forward to program print 'CLIENT:', "\t", $data; } unless ($server and $client and $input and $output) { close $_ for $input, $output, $client, $server; # Exit if program handles closed waitpid $pid, 0; exit; } }