http://www.perlmonks.org?node_id=887907

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

i have a client/server tcp socket script that works but i would like to send stdout from server back to client. for example i send ls /tmp to server i would like to see the ls output on the client. any help would be great

Client code:

#!/usr/bin/env perl use IO::Socket; use strict; my $socket = new IO::Socket::INET ( PeerAddr => $_, PeerPort => '7071', Proto => 'tcp', ); die "Coudn't open socket" unless $socket; $socket->autoflush(1); my $dir = "/tmp"; print $socket "ls:$dir\n"; close($socket);

server code:

#!/usr/bin/env perl # perl modules use IO::Socket; use POSIX qw(:sys_wait_h); use Sys::Hostname; my $hostname = hostname; my $port = "7071"; $SIG{CHLD} = 'IGNORE'; # make socket connection my $socket = new IO::Socket::INET ( LocalHost => "$hostname", LocalPort => "$port", Proto => 'tcp', Reuse => 1, Listen => SOMAXCONN, ); die "Could not create socket: $!\n" unless $socket; while ($client = $socket->accept()) { $client->autoflush(1); while (<$client>) { chomp; my ($command,@var2) = split(":", $_); my $cmd; $pid = fork; if ($pid == 0){ } else { `kill $pid 2>/dev/null`; } unless ( $pid ){ if ($command eq 'ls'){ $cmd = sprintf("ls -s @var"); } $run_cmd = system($cmd); } } close($client); } close($socket);