gpt4 book ai didi

macos - Perl 套接字在执行命令后关闭

转载 作者:行者123 更新时间:2023-12-03 11:56:56 27 4
gpt4 key购买 nike

它是一个简单的 Perl 套接字服务器。它监听给定端口上的命令,如果命令是“播放”,则执行一个applescript。下面的代码有两个问题。

  • 一旦执行“播放”命令,客户端和服务器都将关闭。 (我希望服务器继续监听命令,无论是“播放”还是其他)
  • 套接字不会解除绑定(bind)。 (每次重用都得改端口号)

  • 我在 Mac 上使用终端来执行 perl 脚本,并通过另一台 Mac 上的终端 telnet 来触发它。
    #!/usr/bin/perl -w 
    use warnings;
    use IO::Socket;
    use Net::hostent;

    $PORT = 8000;

    $server = IO::Socket::INET->new( Proto => 'tcp',
    LocalPort => $PORT,
    Listen => 5,
    Reuse => 1) or die "can't setup server" unless $server;

    print "SERVER Waiting on port $PORT\n";

    while ($client = $server->accept()) {
    $client->autoflush(1);
    print $client "Command:\r\n";

    while (<$client>) {
    if (/play/i) {
    exec("osascript '/Users/user/Desktop/play.app'");
    } else {
    print $client "invalid command\r\n";
    }
    } continue {
    print $client "Command: ";
    } close $client;
    }

    最佳答案

    您正在使用 exec .来自 perldoc -f exec

    The exec function executes a system command and never returns; use system instead of exec if you want it to return. It fails and returns false only if the command does not exist and it is executed directly instead of via your system's command shell (see below).

    Since it's a common mistake to use exec instead of system, Perl warns you if there is a following statement that isn't die, warn, or exit (if -w is set--but you always do that, right?). If you really want to follow an exec with some other statement, you can use one of these styles to avoid the warning:



    这基本上意味着您的套接字关闭,因为 perl 脚本退出了。我认为这会使端口被阻塞,因为套接字永远不会显式或隐式关闭。

    关于macos - Perl 套接字在执行命令后关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8326799/

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