use strict; use IO::Socket; use IO::Select; my $local_port = 5000; my $proxy_host = "localhost"; my $proxy_port = 6000; # Used to store client and proxy connections my %client_proxy; my $server = IO::Socket::INET->new( Listen => 5, LocalAddr => 'localhost', LocalPort => $local_port, Proto => 'tcp') or die "Cant create server socket: !"; # Create a select object my $read = new IO::Select; $read->add($server); while (1){ my ($readable) = IO::Select->select($read, undef, undef, 0); foreach my $fh (@$readable){ if ($fh == $server){ # We got a new client, so set up it's configuration my $client = $server->accept; my $proxy = IO::Socket::INET->new( PeerAddr => $proxy_host, PeerPort => $proxy_port, Proto => 'tcp') or die "cannot create proxy socket: $!"; # You probably don't really want to die here. $read->add($client); $read->add($proxy); $client_proxy{$client} = $proxy; $client_proxy{$proxy} = $client; } else { my $buf = <$fh>; if ($buf){ # We need to send some information my $fh2 = $client_proxy{$fh}; print $fh2 $buf; } else { # We need to close our connections my $fh2 = $client_proxy{$fh}; $read->remove($fh2); close ($fh2); delete $client_proxy{$fh2}; $read->remove($fh); close ($fh); delete $client_proxy{$fh}; } } } }