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!

In reply to IO::Select client and server in one by amir

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.