I tried the suggestion and still got nothing back.
client code:
#!/usr/bin/env perl
use IO::Socket;
$remote_port=7071;
$remote_host="localhost ";
$socket = IO::Socket::INET->new(PeerAddr => $remote_host,
PeerPort => $remote_port,
Proto => "tcp",
Type => SOCK_STREAM)
or die "Couldn't connect to $remote_host:$remote_port : $@\n";
$socket->autoflush(1);
# ... do something with the socket
print $socket "ls:/tmp\n";
while (<$socket>) {
print "$_\n";
}
# and terminate the connection when we're done
close($socket);
Server code:
#!/usr/bin/env perl
# perl modules
use IO::Socket;
use POSIX qw(:sys_wait_h);
use Sys::Hostname;
use IPC::Open3 qw( open3 );
# get hostname
my $hostname = hostname;
# Port
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;
# allow server to accept clients
while ($client = $socket->accept()) {
# flush input
$client->autoflush(1);
# input from client
while (<$client>) {
chomp;
# read command and put rest in array
my ($command,@var2) = split(":", $_);
my $cmd;
if ($command eq 'ls'){
@cmd = ( 'ls', '-s', '--', @var2 );
}
# run command
if (@cmd) {
open(local *C_STDIN, '<', '/dev/null')
+ or die;
open(local *C_STDIN, '>&', $client) or
+ die;
my $pid = open3('C_STDIN', '>C_STDOUT'
+, 'C_STDOUT', @cmd);
waitpid($pid, 0);
}
}
# close client
close($client);
}
# close socket so it can reused
close($socket);
|