perl - 无法从 perl 中的套接字读取 - 可能是死锁?

标签 perl sockets

我的操作系统是带有 perl 5.14.2 的 Archlinux。我只是想写一个小程序来完成远程通信。该程序只是将一个 C 源文件传递给服务器。服务器将调用 gcc 来编译 C 代码并传递编译器的消息。客户端收不到编译器的消息。我在服务器中有消息。 有代码:

#!/usr/bin/perl -w
# oj.pl --- alpha


use warnings;
use strict;
use IO::File;
use IO::Socket;

use  constant MY_TRAN_PORT => 138000;
$| = 1;


my $tmpFileToBeCompiled = IO::File->new ("> tmpFile09090989.c") or die "Can't creat this file";

#if (defined $tmpFileToBeCompiled) {
#    print $tmpFileToBeCompiled "argh";         # just for test!
#}
# $fihi->close;

my $port        = shift || MY_TRAN_PORT;

my $sock_server = IO::Socket::INET->new (Listen         => 20,
                                         LocalPort      => $port,
                                         Timeout        => 60,
                                         Reuse          => 1)
    or die "Can't create listening socket: $!\n";

my $tmp = 1;
while ($tmp) {
    next unless my $session = $sock_server->accept;

    my $peer = gethostbyaddr ($session->peeraddr, AF_INET)
        || $session->peerhost;
    warn "Connection from [$peer, $port]\n";

    while (<$session>) {
        print $tmpFileToBeCompiled $_;              # if it works, the filehandle should be changed into tmpFile.  just fixed.
        print $session "test!";

    }

    my @lines = `gcc tmpFile09090989.c 2>&1`;

    foreach ( @lines) {
        print $session  $_ . "test!!!\n";
     #   $session->print;
    }


    print "OK!";
    $tmpFileToBeCompiled->close;

    warn "Connecting finished!\n";
    $session->close;
    $tmp --;
}

$sock_server->close;

----------------------------------------end--------------------------------------------------------
-------------------------------------client.pl--------------------------------------------------------
use warnings;
use strict;

use IO::Socket qw(:DEFAULT);
use File::Copy;
use constant MY_TRAN_PORT => 138000;
use IO::File;

my $host = shift || '127.0.0.1';
my $port = shift || MY_TRAN_PORT;

my $socket = IO::Socket::INET->new("$host:$port") or die $@;

my $fh = IO::File->new("a.c", "r");

my $child = fork();
die "Can't fork: $!\n" unless defined $child;

# if (!$child) {
#     $SIG{CHLD} = sub { exit 0 };
#     userToHost();
#     print "Run userToHost done!\n";

#     $socket->shutdown(1);
#     sleep;
# } else {
#     hostToUser();
#     print "Run hostToUser done! \n";

#     warn "Connection closed by foreign host\n";

# }
userToHost();
unless ($child) {
    hostToUser();
    print "Run hostToUser done! \n";
    warn "Connection closed by foreign host\n";
    $socket->close;

}

sub userToHost {
    while (<$fh>) {
#    print $_;    # for debug
    print $socket $_;
    }
}

sub hostToUser {
    while (<$socket >) {
    print $_;    
    }
}
# copy ("a.c", $socket) or die "Copy failed: $!";
print "Done!";

最佳答案

  1. 您不需要 fork 客户端。完全没有。正如主题所说
  2. 您的客户端代码有误:<$socket >应该是 <$socket>
  3. 您需要通知服务器您已经写入所有数据,服务器可以开始编译。否则服务器将停留在 while (<$session>)永远。 为此,您可以调用 shutdown($socket, 1)这意味着你写完了。参见 perldoc -f shutdown

最终原型(prototype)(非常粗糙)可能如下所示:https://gist.github.com/19b589b8fc8072e3cfff

关于perl - 无法从 perl 中的套接字读取 - 可能是死锁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8111673/

相关文章:

用于后处理的 perl 代码结构

python - 如何在 Python 中获取套接字的外部 IP?

c# - ProcessStartInfo 无法调用 perl.exe

python - 从相对路径确定绝对路径

perl - 根据字符串出现的次数拆分一行

python - 通过套接字在C++和python之间交换固定 float 组

c# - 使用二进制协议(protocol)的 TCP 成帧

带有 python 套接字的 iPhone 客户端

windows - Socket.Bind 和 IP 源路由,具有多个本地网络接口(interface)

regex - perl - 正则表达式之间的范围