in reply to Sockets and threads, oh my!
Your socket is in blocking mode which means that you won't be able to write to it until your sysread in the thread times out--which it never will. The solution is to use Tye's method of setting the thread non-blocking
Update: made the listener detect when the other end disconnected.
#!/usr/bin/perl5.8.8 -w use strict; use warnings; use threads; use IO::Socket qw(:DEFAULT :crlf); my $host = shift || 'localhost'; my $socket = IO::Socket::INET->new( PeerAddr => $host, PeerPort => 'ftp', Proto => 'tcp', Type => SOCK_STREAM, Timeout => 5 ) or die "Couldn't connect to remote host: $@\n"; ioctl( $socket, 0x8004667e, \1 ); my $listenerThread = threads->create(\&listener, $socket) or die "$@\n +"; while (<STDIN>) { chomp; print $socket $_, CRLF; }; exit(0); sub listener($) { my $s = shift; my $data; $/ = CRLF; while( 1 ) { ## try to read something. my $read = sysread($s, $data, 2048); ## Quit if sysread gave an error (returned undef) other than 1 +0035 ## 10035 => A non-blocking socket operation could not be comp +leted immediately last unless defined $read or 0+$^E == 10035; ## If there was nothing to read, sleep a while and retry sleep 1 and redo unless $read; ## Got something, so display it syswrite(STDOUT, $data); } print "\nListener exiting\n"; }; __END__
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Sockets and threads, oh my!
by KevinZwack (Chaplain) on Jun 19, 2006 at 21:07 UTC | |
by BrowserUk (Patriarch) on Jun 19, 2006 at 21:44 UTC | |
by KevinZwack (Chaplain) on Jun 20, 2006 at 17:19 UTC |