如何在 Perl 中处理客户端服务器通信?

发布于 2024-12-18 22:42:24 字数 1386 浏览 0 评论 0 原文

我有两个 Perl 脚本,它们应该执行以下工作:

服务器启动并等待客户端连接。 客户端连接并立即发送 XML 文件。 服务器读取 XML 文件并启动几个线程。 每个线程接收 $client 套接字作为参数,一旦完成,它们必须将输出发送回客户端。 同时,客户端正在等待消息到达,他应该在命令行上打印这些消息。

现在我的问题是我不知道如何实现最后一部分。我尝试使用接收方法,但如果我这样做,两个程序都会卡住。

服务器端:

my @threads;
$server = IO::Socket::INET->new(
            Listen => 1,                
            LocalAddr => 'localhost', 
            LocalPort => '8080', 
            Proto => 'tcp'
) or die "Can't crete socket!\n";
$client = $server->accept;
open FILE, ">".$filename or die "Can't open file!\n";
while (<$client>) {
    print FILE $_;  
}
close FILE;

# READ FILE AND DO SOME OTHER STUFFS
push @threads, threads ->create(\&subroutine1, $parameters, $client);
# OTHER STUFFS
push @threads, threads ->create(\&subroutine2, $parameters, $client);
# MORE THREADS...

sub subroutine1 {
    my @pars= @_;
    my $parameters = $pars[0];
    my $client = $pars[1];
    my $sub1 = `perl other_script.pl $parameters`;
    $client->send($mem);
}

客户端:

my $server = IO::Socket::INET->new(
    PeerAddr => 'localhost',
    PeerPort => '8080',
    Proto => 'tcp'
) or die "Can't create client socket!\n";
open FILE, $filename;
while (<FILE>) {
    print $server $_;   
}
close FILE;
# HOW DO I READ INFORMATIONS?
$server->close();

I have two Perl scripts that are supposed to do the following job:

Server starts and wait for the client to connect.
The client connects and istantly send an XML file.
Server reads XML file and starts a couple of threads.
Each thread receive $client socket as a parameter and once they're done they have to send the output back to the client.
Meanwhile the client is waiting for messages to arrive, he's supposed to print those messages on the command line.

Now my problem is that I don't know how to implement the last part. I tryed to use the receive method but both programs get stuck if I do that.

Server side:

my @threads;
$server = IO::Socket::INET->new(
            Listen => 1,                
            LocalAddr => 'localhost', 
            LocalPort => '8080', 
            Proto => 'tcp'
) or die "Can't crete socket!\n";
$client = $server->accept;
open FILE, ">".$filename or die "Can't open file!\n";
while (<$client>) {
    print FILE $_;  
}
close FILE;

# READ FILE AND DO SOME OTHER STUFFS
push @threads, threads ->create(\&subroutine1, $parameters, $client);
# OTHER STUFFS
push @threads, threads ->create(\&subroutine2, $parameters, $client);
# MORE THREADS...

sub subroutine1 {
    my @pars= @_;
    my $parameters = $pars[0];
    my $client = $pars[1];
    my $sub1 = `perl other_script.pl $parameters`;
    $client->send($mem);
}

Client side:

my $server = IO::Socket::INET->new(
    PeerAddr => 'localhost',
    PeerPort => '8080',
    Proto => 'tcp'
) or die "Can't create client socket!\n";
open FILE, $filename;
while (<FILE>) {
    print $server $_;   
}
close FILE;
# HOW DO I READ INFORMATIONS?
$server->close();

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

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

发布评论

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

评论(1

浮光之海 2024-12-25 22:42:24

您不能将发送部分放入子线程/工作线程中,它会变得混乱。

问:多线程鸡为什么要过马路?

A:另一边。前往获取

,让父/主线程收集子线程的结果,必要时对数据进行排序,序列化,然后作为整体发送。

您可以使用 recv 函数 进行读取,也可以作为 IO::Socket

You cannot put the send part into the children/worker threads, it'll be jumbled.

Q: Why did the multi-threaded chicken cross the road?

A: other to side. To the get

Instead, have the parent/boss thread collect the results of the children, sort the data if necessary, serialise, then send as a whole.

You read with the recv function, also available as a method in IO::Socket.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文