1

ゲームサーバーを制御して、その出力をリアルタイムで表示しようとしています。これは私がこれまでに持っているものです:

#!/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;
 }

それはプロセスをうまく開始します、唯一の欠点はそれがJavaプロセスの出力をソケットに出力しないことです(それはPerlが開始されたターミナルウィンドウに出力を表示しています)私はpingでこれを試しましたそしてそれはうまくいきましたいいでしょう、何かアイデアはありますか?

前もって感謝します!

4

2 に答える 2

1

Javaコード(またはおそらくそれscreen)が標準エラーストリームに出力を出力しているようです。個別にキャプチャしたくないと仮定すると、簡単な修正は次のとおりです。

それを抑制します:

open(JAVA, "screen java -jar craftbukkit-0.0.1-SNAPSHOT.jar 2>/dev/null |")

標準出力ストリームにキャプチャします。

open(JAVA, "screen java -jar craftbukkit-0.0.1-SNAPSHOT.jar 2>&1 |")

于 2011-03-08T23:45:58.577 に答える
0

screen出力を「画面」にリダイレクトします。コマンド内のを削除しscreenます。

于 2011-03-09T01:12:01.800 に答える