#!/opt/threadperl/bin/perl -w # threaded client with perl 5005 threads use IO::Socket; use Thread; use strict; die "Usage $0 host port" unless @ARGV == 2; my $host = shift; my $port = shift; my $cr = "\015"; my $lf = "\012"; my $crlf = "$cr$lf"; my $done = undef; # used to send messages between threads my $sock = new IO::Socket::INET ( PeerAddr=>$host, PeerPort=>$port, Proto=>'tcp', ); die "Socket: $!" unless $sock; print STDERR "[Connected to $host at $port]\n"; my $thr = new Thread \&from_server, $sock; # get info from server while ( defined (my $line = ) ) { # parent thread sends info to server chomp $line; $line .= $crlf; if ( $done ) { print STDERR "Host has closed connection\n"; last; } print $sock $line; last if ($line =~ /^(quit|exit)/); } $thr->join; # get the exit code from kid thread close $sock; exit; sub from_server { # print STDERR "Starting new thread " . Thread->self->tid() . "\n"; my $sock = shift; while ( defined (my $line = <$sock>) ) { print $line; }; $done = 1; }