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";
|