Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perl server output to client

Status
Not open for further replies.

piperent

Programmer
Feb 7, 2002
156
0
0
US
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:

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



 
system() does not return output back to your perl program as is clearly stated in the system() functions man page.

The return value is the exit status of the program as returned by the wait call. To get the actual exit value, shift right by eight (see below). See also "exec". This is not what you want to use to capture the output from a command, for that you should use merely backticks or qx//, as described in "`STRING`" in perlop.



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks for the response Kevin. I re-read the 'system' command manual, and what you say is correct, but, I have also tried other methodology to no avail. I tried the 'string' thing (as well as the exec) and they didn't solve my problem.
When I use backticks and/or qx// all I get are errors and warnings to the effect that it can't find the 'string end sentinel' before it reaches EOF.

It has to be in the syntax, but who knows. Obviously there is a way to capture the output of a command and then pipe that across to a client, but for all my efforts I'm still batting '0'.

Thanks for your efforts.

JP
 
chomp ($data = `cat /tmp/pretest`) #if need to check the size of /tmp/pretest first then add this; if (-s "/tmp/pretest" > 0);
print "$data";
or
print CLIENT "$data\n"; # I'm assuming CLIENT is a FH here, since I don't see it in the code.
 
Thanks to Max1x ...... this suggestion does work. I had to add an obligatory 'my' to the $data filehandle, but once done it worked as expected. Still not real clear on all the idiosyncracies, but now I have something to work from. I can figure out the why's and where-fores from there.

Thanks again.

JP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top