加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

套接字 – perl6 IO ::套接字::异步服务器死亡,例外:peer通过pe

发布时间:2020-12-15 21:55:56 所属栏目:大数据 来源:网络整理
导读:这是echo服务器代码: #!/usr/bin/env perl6my $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: hellon") ; whenever $conn.Supply.l
这是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: hellon") ;
        whenever $conn.Supply.lines -> $line {
            say "$id: $line" ;
            $conn.print( "$id : $linen") ;
        }
    }
}

这是客户端代码:

#!/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

如何优雅地捕获网络错误,以便服务器更强大?

解决方法

如果你想在每次退出时(或者当Promise被破坏时)底层的Supply(或任何等待的,如Promise)处理这种情况,你可以在随时随地安装一个QUIT处理程序.它的工作原理很像异常处理程序,因此它会要求您以某种方式匹配异常,或者只是默认情况下,如果您想将所有退出原因视为“罚款”.
whenever $conn.Supply.lines -> $line {
    say "$id: $line" ;
    $conn.print( "$id : $linen") ;
    QUIT {
        default {
            say "oh no,$_";
        }
    }
}

这将输出“哦不,由同行重置连接”并继续运行.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读