perl6 IO::Socket::INET Could not receive data from socket: Connection reset by peer

大兔子大兔子 提交于 2019-12-07 19:36:03

问题


Sample server:

#!/usr/bin/env perl6
my $listen = IO::Socket::INET.new(:listen, :localhost<localhost>, :localport(3333));
loop {
    my $conn = $listen.accept;
    while my $buf = $conn.recv(:bin) {
        $conn.write: $buf;
    }
    $conn.close;
}

Client:

#!/use/bin/env perl6
my $c = IO::Socket::INET.new(:host<localhost>, :port(3333));
$c.print: "{time}\n";
#say $c.recv; #commented out on purpose
sleep 1 ;
$c.close ;

server error:

Could not receive data from socket: Connection reset by peer in block <unit> at server4.p6 line 5

In the server on each of the blocks I tried CATCH and QUIT. How should I catch this error?


回答1:


Server needs to catch the error in the loop block:

#!/usr/bin/env perl6
my $listen = IO::Socket::INET.new(:listen, :localhost<localhost>, :localport(3333));
loop {
    my $conn = $listen.accept;
    while my $buf = $conn.get {
        $conn.print: $buf;
    }
    $conn.close;
    CATCH { default { say .^name, ': ', .Str ,  " handled in $?LINE";}}
}

Output of server reports the error and stays running to accept new connections:

perl6 --ll-exception server.p6
X::AdHoc: Could not receive data from socket: Connection reset by peer handled in 9


来源:https://stackoverflow.com/questions/50032392/perl6-iosocketinet-could-not-receive-data-from-socket-connection-reset-by-p

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!