从 Perl 管道中读取不断输出的文本

发布于 2024-10-20 19:45:20 字数 1683 浏览 4 评论 0原文

我最近尝试用 Perl 制作一个游戏服务器控制器,我想启动、停止和查看游戏服务器输出的文本,这就是我到目前为止所拥有的:

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

 $PORT = 9050;                  # 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 (/some|thing/i) {
        printf $client "%s\n", scalar localtime;  }
     elsif (/start/i ) {
        open RSPS, '|java -jar JARFILE.jar' or die "ERROR STARTING: $!\n";
        print  $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 "Hmmmm\n";
     }
   } continue {
      print $client "Command? ";
   }
   close $client;
 }

我在从管道读取数据时遇到问题,任何想法?

谢谢!

I recently tried to make a game server controller in Perl, I would like to start, stop and view the text that has been outputted by the game server, this is what I have so far:

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

 $PORT = 9050;                  # 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 (/some|thing/i) {
        printf $client "%s\n", scalar localtime;  }
     elsif (/start/i ) {
        open RSPS, '|java -jar JARFILE.jar' or die "ERROR STARTING: $!\n";
        print  $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 "Hmmmm\n";
     }
   } continue {
      print $client "Command? ";
   }
   close $client;
 }

I am having trouble reading from the pipe, any ideas?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不在是我 2024-10-27 19:45:20

您正在尝试对 RSPS 文件句柄进行读取和写入操作,尽管您只是打开它进行写入(open RSPS, '|java -jar JARFILE.jar' 表示启动 java 进程并使用 RSPS 文件句柄写入 java 进程的标准输入)。

要读取进程的输出,您需要将进程输出写入文件并打开该文件的单独文件句柄

open RSPS, '| java -jar JARFILE.jar > jarfile.out';
open PROC_OUTPUT, '<', 'jarfile.out';

,或者查看诸如 IPC::Open3,这是为这样的应用程序制作的。

use IPC::Open3;
# write to RSPS and read from PROC_OUTPUT and PROC_ERROR
open3(\*RSPS, \*PROC_OUTPUT, \*PROC_ERROR,
      'java -jar JARFILE.jar');

You are trying to do both reading and writing on the RSPS filehandle, though you have only opened it for writing (open RSPS, '|java -jar JARFILE.jar' means start the java process and use the RSPS filehandle to write to the standard input of the java process).

To read the output of the process, you will either need to write the process output to a file and open a separate filehandle to that file

open RSPS, '| java -jar JARFILE.jar > jarfile.out';
open PROC_OUTPUT, '<', 'jarfile.out';

or check out a module like IPC::Open3, which was made for applications like this.

use IPC::Open3;
# write to RSPS and read from PROC_OUTPUT and PROC_ERROR
open3(\*RSPS, \*PROC_OUTPUT, \*PROC_ERROR,
      'java -jar JARFILE.jar');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文