示例服务器:
#!/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 ;
客户端:
Could not receive data from socket: Connection reset by peer
in block <unit> at server4.p6 line 5
服务器错误:
CATCH
在每个块的服务器上,我尝试了QUIT
和sort
。我该如何捕捉到这个错误?
答案 0 :(得分:3)
服务器需要捕获循环块中的错误:
#!/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";}}
}
服务器输出报告错误并保持运行以接受新连接:
perl6 --ll-exception server.p6
X::AdHoc: Could not receive data from socket: Connection reset by peer handled in 9