That was how I wrote the first incarnation (I think at least).
I am not polling for writable handles because in my previous experiences that was mostly unnecessary.
The while loop exits immediately with nothing in @ready. That problem led me to believe that IO::Select was not happy with filehandles. At any rate, here is the complete script:
use IO::Socket; use IO::Select; use IO::Handle; use strict; use warnings; my ($stdin, $stdout); my ($t_conn, $t_addr, $t_port, $t_buf); my ($buf, @ready, $socket, $sel, $ret); $t_addr = shift; $t_port = shift; $stdin = new IO::Handle; $stdin->fdopen(fileno(STDIN), 'r') $stdout = new IO::Handle; $stdout->fdopen(fileno(STDOUT), 'w') $t_conn = IO::Socket::INET->new(PeerAddr => $t_addr, PeerPort => $t_port, Proto => 'tcp'); if ( $t_conn->connected() ) { print 'connected'."\n"; } else { die 'could not establish a connection...'."\n"; } $t_conn->syswrite('Connected!'."\n"); $sel = new IO::Select(); $sel->add($t_conn); $sel->add($stdin); $stdout->syswrite('begin transmissions...'."\n"); print $sel->count()."\n"; while ( ( $sel->count() > 0 ) && ( @ready = $sel->can_read() ) ) { if ( length($buf) > 0 ) { $t_conn->syswrite($buf); } foreach $socket (@ready) { if ( $socket == $t_conn ) { if ( $t_conn->connected() ) { if ( $t_conn->sysread($buf, 1024) > 0 ) { $stdout->syswrite($buf); } else { print 'lost connection to tunnel, shutting down... +'."\n"; $sel->remove($t_conn); $t_conn->close(); $stdin->close(); $stdout->close(); } } else { print 'lost connection to tunnel, shutting down...'."\ +n"; $sel->remove($t_conn); $t_conn->close(); $stdin->close(); $stdout->close(); } } elsif ( $socket == $stdin ) { if ( $stdin->opened() ) { if ( $stdin->sysread($buf, 1024) > 0 ) { $t_conn->syswrite($buf); } else { print 'lost connection to host, shutting down...'. +"\n"; $sel->remove($t_conn); $t_conn->close(); $stdin->close(); $stdout->close(); } } else { print 'lost connection to host, shutting down...'."\n" +; $sel->remove($t_conn); $t_conn->close(); $stdin->close(); $stdout->close(); } } } $buf = ''; } print 'all connections closed... exiting...'."\n@ready\n".$sel->handle +s()."\n";

In reply to RE: Re: stdin-socket without fork()?? by gharris
in thread stdin-socket without fork()?? by gharris

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.