use IO::Socket; use IO::Select; $local_port = 5000; $proxy_host = "localhost"; $proxy_port = 6000; $server = IO::Socket::INET->new( Listen => 5, LocalAddr => 'localhost', LocalPort => $local_port, Proto => 'tcp') or die "Cant create server socket: !"; # Create a select object $read = new IO::Select; while ($client = $server->accept) { $proxy = IO::Socket::INET->new( PeerAddr => $proxy_host, PeerPort => $proxy_port, Proto => 'tcp') or die "cannot create proxy socket: $!"; # Unbuffer our output $oldfh = select ($proxy); $| = 1; select ($client); $| = 1; select ($oldfh); # Add our filehandles to our select object $read->add($client); $read->add($proxy); # This will wait until one of the filehandles has something to read while (defined (($readable) = IO::Select->select($read,undef , undef, 0))){ foreach $fh (@$readable) { # The client has something to say if ($fh == $client) { $buf = <$client>; if ($buf){ # Lets tell the proxy print $proxy $buf; } else { # The client closed last; } # The proxy has something to say } else { $buf = <$proxy>; if ($buf){ # Lets tell the client print $client $buf; } else { # The proxy closed last; } } } } # Remove our file handles from select and close them $read->remove($client); $read->remove($proxy); $client->close; $proxy->close; } # Now we go onto the next client