简体   繁体   中英

Perl Pipe not redirecting Java process output

I'm trying to control a game server and display it's output in real time. This is what I have so far:

#!/usr/bin/perl -w
 use IO::Socket;
 use Net::hostent;              # for OO version of gethostbyaddr

 $PORT = 9000;                  # pick something not in use

 $server = IO::Socket::INET->new( Proto     => 'tcp',
                                  LocalPort => $PORT,
                                  Listen    => SOMAXCONN,
                                  Reuse     => 1);

 die "can't setup server" unless $server;
 print "[Server $0 accepting clients]\n";

 while ($client = $server->accept()) {
   $client->autoflush(1);
   print $client "Welcome to $0; type help for command list.\n";
   $hostinfo = gethostbyaddr($client->peeraddr);
   printf "[Connect from %s]\n", $hostinfo->name || $client->peerhost;
   print $client "Command? ";

   while ( <$client>) {
     next unless /\S/;       # blank line
     if    (/quit|exit/i) {
        last;                                     }
     elsif (/fail|omg/i) {
        printf $client "%s\n", scalar localtime;  }
     elsif (/start/i ) {
        if (my $ping_pid = open(JAVA, "screen java -jar craftbukkit-0.0.1-SNAPSHOT.jar |")) {
    while (my $ping_output = <JAVA>) {
        # Do something with the output, let's say print
        print $client $ping_output;

        # Kill the C program based on some arbitrary condition (in this case
        # the output of the program itself).
                }
        }
        printf $client "I think it started...\n Say status for output\n";                }
     elsif (/stop/i ) {
        print RSPS "stop";

    close(RSPS);
        print  $client "Should be closed.\n"; }
     elsif (/status/i ) {
        $output = RSPS;
        print $client $output;      }
     else {
       print $client "FAIL\n";
     }
   } continue {
      print $client "Command? ";
   }
   close $client;
 }

It starts the process just fine, the only flaw is that it's not outputting the output of the Java process to the socket (It is displaying the output in the terminal window that Perl was initiated with) I've tried this with ping and it worked just fine, any ideas?

Thanks in advance!

It sounds like the Java code (or maybe it's screen ) is printing some output to the standard error stream. Assuming that you don't want to capture it separately , some easy fixes are:

Suppress it:

open(JAVA, "screen java -jar craftbukkit-0.0.1-SNAPSHOT.jar |")

Capture it in the standard output stream:

open(JAVA, "screen java -jar craftbukkit-0.0.1-SNAPSHOT.jar |")

screen redirects the output to the "screen". Get rid of the screen in the command.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM