perl6 IO::Socket::INET Could not receive data from

2019-05-15 13:56发布

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?

标签: sockets perl6
1条回答
迷人小祖宗
2楼-- · 2019-05-15 14:03

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
查看更多
登录 后发表回答