I'm new to Perl and have a simple server running under SCO 5.0.5. All this server does is wait for a client to connect and then sends a short text message back to the client. As long as I only send a brief 'hard-coded' message back to the client, everything works as expected.
My question is, how do you re-direct or pipe output from a Unix command back over to the client. I can use a 'system' command to execute a 'cat fileA', but the output only goes to STDOUT. What am I missing?
This is the server code:
I've tried the 'system(......)' command line in a number of different ways, but the output always comes back to my Unix console rather than across to the client. '/tmp/perltest' is just a simple text file.
Thanks for any assistance.
JP
My question is, how do you re-direct or pipe output from a Unix command back over to the client. I can use a 'system' command to execute a 'cat fileA', but the output only goes to STDOUT. What am I missing?
This is the server code:
Code:
# use port 7890 as default
my $port = shift || 7890;
my $proto = getprotobyname('tcp');
# create a socket, make it reusable
socket(SERVER, PF_INET, SOCK_STREAM, $proto)
or die "socket: $!";
setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1)
or die "setsock: $!";
# grab a port on this machine
my $paddr = sockaddr_in($port, INADDR_ANY);
# bind to a port, then listen
bind(SERVER, $paddr) or die "bind: $!";
listen(SERVER, SOMAXCONN) or die "listen: $!";
print "SERVER started on port $port\n";
# for each connection...
my $client_addr;
while ($client_addr = accept(CLIENT, SERVER)) {
# find out who connected
my ($client_port, $client_ip) =
sockaddr_in($client_addr);
my $client_ipnum = inet_ntoa($client_ip);
my $client_host =
gethostbyaddr($client_ip, AF_INET);
# tell who connected
print "got a connection from: $client_host",
#print "got a connection from: ",
" [$client_ipnum]\n";
# sent them a message, close connection
[purple]
print CLIENT "Hello - server here - speaking to you: ";
system( 'cat /tmp/perltest') == 0
or die "system cat failed: $?" ;
[/purple]
close CLIENT;
}
I've tried the 'system(......)' command line in a number of different ways, but the output always comes back to my Unix console rather than across to the client. '/tmp/perltest' is just a simple text file.
Thanks for any assistance.
JP