问题
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?
回答1:
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
来源:https://stackoverflow.com/questions/50032392/perl6-iosocketinet-could-not-receive-data-from-socket-connection-reset-by-p