gpt4 book ai didi

perl - 从 Perl 管道读取不断输出文本

转载 作者:行者123 更新时间:2023-12-04 06:32:34 25 4
gpt4 key购买 nike

我最近尝试在Perl中制作一个游戏服务器 Controller ,我想启动,停止和查看游戏服务器已经输出的文本,这是我到目前为止的:

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

我无法从管道中读取,有什么想法吗?

谢谢!

最佳答案

您正在尝试在 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');

关于perl - 从 Perl 管道读取不断输出文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5221269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com