amir has asked for the wisdom of the Perl Monks concerning the following question:

I'm writing a (proxy) server in Perl that is a kind of TCP tunnel, but will work with IPv6. It will:
- connect to an IPv6-connected server (IRC or SMTP for example)
- a client will communicate with our server, which transparently proxies all data (in real-time, no buffering) to the "real" server

Here is the code I have so far (at the moment, the client is just the STDIN and STDOUT filehandles), but I can't get it to even print out what the remote server is saying!

use Socket; use Socket6; use IO::Select; $| = 1; my($host,$port) = ("irc.weblook2k.com",6667); @res = getaddrinfo($host, $port, AF_UNSPEC, SOCK_STREAM); $family = -1; ($family, $socktype, $proto, $saddr, $canonname, @res) = @res; ($host, $port) = getnameinfo($saddr, NI_NUMERICHOST | NI_NUMERICSERV); socket(SOCK, $family, $socktype, $proto); connect(SOCK, $saddr) or die("connect error!"); my $s = new IO::Select(); $s->add(\*STDIN); $s->add(\*SOCK); while(1) { my $rh_set = IO::Select->select($s, $s, undef, 0.1); my $rh; foreach $rh (@$rh_set) { if($rh == $s) { $ns = $rh->accept(); $s->add($ns); } else { # read data $buf = <$rh>; # should use sysread() maybe? if($buf) { print STDOUT $buf; } else { $rh_set->remove($rh); close($rh); } } } }
So, when something is entered in STDIN, it should be sent to the server, when the server sends something, it should be printed to STDOUT. Any help, code :), tips would be greatly appreciated!

Replies are listed 'Best First'.
Re: IO::Select client and server in one
by fokat (Deacon) on Aug 01, 2002 at 02:31 UTC
    It's always a good idea to use sysread() and syswrite() whenever you use some form of select().