这是echo服务器代码:
#!/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;
当客户端连接然后不从服务器检索任何数据,然后关闭服务器因此错误而死的连接:
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
如何优雅地捕获网络错误,以便服务器更强大?