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);
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.