perl6 IO :: Socket ::异步服务器死于以下情况:连接被对等方重置 [英] perl6 IO::Socket::Async server dies with the exception: connection reset by peer
问题描述
这是回显服务器代码:
#!/usr/bin/env perl6
my $port = 3333 ;
say "listen port $port" ;
react {
my $ids = 0 ;
whenever IO::Socket::Async.listen('0.0.0.0', $port ) -> $conn {
my $id = $ids++ ;
$conn.print( "$id: hello\n") ;
whenever $conn.Supply.lines -> $line {
say "$id: $line" ;
$conn.print( "$id : $line\n") ;
}
}
}
这是客户端代码:
#!/usr/bin/env perl6
my $port = 3333 ;
my $conn = await IO::Socket::Async.connect('localhost', $port );
$conn.print: "{time}\n";
#$conn.Supply.tap(-> $v { print $v });
sleep 1 ;
$conn.close;
当客户端连接然后不从服务器检索任何数据时,然后关闭连接,服务器因以下错误而死:
When the client connect then does not retrieve any data from the server, then closes the connection the server dies with this error:
listen port 3333
0: 1524671069
An operation first awaited:
in block <unit> at ./server2.p6 line 5
Died with the exception:
connection reset by peer
in block <unit> at ./server2.p6 line 5
X::AdHoc+{X::Await::Died}: connection reset by peer
如何优雅地捕获网络错误,以使服务器更强大?
How do I gracefully catch network errors so the server is more robust?
推荐答案
如果您要处理当whenever
下的Supply
(或任何等待的对象,例如Promise
)退出(或当Promise
损坏),您可以随时在其中安装QUIT
处理程序.它的工作原理与异常处理程序非常相似,因此它希望您以某种方式匹配该异常,或者如果您要将所有退出原因都视为罚款",则只需default
.
If you want to handle the case when a Supply
(or any awaitable, like a Promise
) underlying a whenever
quits (or when the Promise
is broken), you can install a QUIT
handler inside the whenever. It works a lot like an exception handler, so it will want you to either match the exception somehow, or just default
if you want to treat all quit causes as "fine".
whenever $conn.Supply.lines -> $line {
say "$id: $line" ;
$conn.print( "$id : $line\n") ;
QUIT {
default {
say "oh no, $_";
}
}
}
这将输出哦,不,连接被对等方重置"并继续运行.
This will output "oh no, connection reset by peer" and continue running.
这篇关于perl6 IO :: Socket ::异步服务器死于以下情况:连接被对等方重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!