Perl 6 udp socket:如何从服务器读取响应?

Shn*_*son 5 sockets perl6

server-udp.pl

my $socket = IO::Socket::Async.bind-udp('localhost', 3333);
react {
    whenever $socket.Supply -> $v {
        if $v.chars > 0 {
            $v.print;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

client-udp.pl

my $socket = IO::Socket::Async.udp();
await $socket.print-to('localhost', 3333, "\nHello, Perl 6!");
Run Code Online (Sandbox Code Playgroud)

客户端如何读取服务器响应?
也许这还没有实现?

例如在Perl 5中:

client.pl

...
my $data_send = "Test 1234567890";
$client_socket->send( $data_send )
    or die "Client error while sending: $!\n";

# read operation
$client_socket->recv( my $data_rcv , 1024 )
    or die "Client error while received: $!\n";

print "Received data: $data_rcv\n";
...
Run Code Online (Sandbox Code Playgroud)

Kai*_*epi 6

在我得到答案之前,除非您不关心从谁接收数据,否则您不会以 UDP 套接字的正确方式侦听服务器上的数据。您应该传递:datagramIO::Socket::Async.Supply,这使得TappablebyIO::Socket::Async.bind-udp返回的对象发出包含接收到的数据以及对等点的主机名和端口的对象,而不是单独的数据:

my IO::Socket::Async::D $server .= bind-udp: 'localhost', 3333;
react whenever $server.Supply(:datagram) -> $datagram {
    print $datagram.data if $datagram.data.chars > 0;
}
Run Code Online (Sandbox Code Playgroud)

用于表示数据报的类型在编写时尚未记录,但它只不过是一个容器,所以这就是它在 Rakudo 中的实现方式:

my class Datagram {
    has $.data;
    has str $.hostname;
    has int $.port;

    method decode(|c) {
        $!data ~~ Str
          ?? X::AdHoc.new( payload => "Cannot decode a datagram with Str data").throw
          !! self.clone(data => $!data.decode(|c))
    }
    method encode(|c) {
        $!data ~~ Blob
          ?? X::AdHoc.new( payload => "Cannot encode a datagram with Blob data" ).throw
          !! self.clone(data => $!data.encode(|c))
    }
}
Run Code Online (Sandbox Code Playgroud)

解决这个问题后,有一种方法可以在不使用 NativeCall 的情况下使用 UDP 监听客户端接收到的数据;IO::Socket::Async.bind-udp并且IO::Socket::Async.udp两者都返回一个IO::Socket::Async实例,因此您可以像在服务器上一样在客户端上侦听消息:

my IO::Socket::Async:D $client .= udp;
react whenever $client.Supply(:datagram) -> $datagram {
    # ...
}
Run Code Online (Sandbox Code Playgroud)


Cur*_*mes 5

首先让我重申一下我上面的评论。通过阅读IO::Socket::Async的文档,我没有看到明显的方法来做到这一点。您可以设置 UDP 发送方或 UDP 接收方,但不能同时设置两者。

UDP 连接由 4 项定义(发送方地址、发送方端口、接收方地址、接收方端口)。

服务器可以侦听给定的地址/端口。收到数据包后,通常有多种方法可以查询发送者的地址/端口。这是我在 Perl 6 中看不到的。

客户端可以将数据包定向到特定的服务器地址/端口。客户端通常选择一个随机的“发送方端口”,给出“连接”所需的第四个元素(在此无连接协议中)。

因此,就像在其他语言的示例中一样,客户端发送数据包,服务器查找发送者的地址/端口,然后将数据包返回到同一地址/端口。客户端发送数据包后,再次监听发送数据包的同一随机端口,以接收来自服务器的响应。我在 Perl 6 中没有看到明显的方法可以在刚刚发送到的同一端口上跟进print-toa 。recv

话虽如此,Perl 6 有一个很棒的NativeCall工具,可以用来直接调用动态库,所以如果您愿意的话,您可以使用实际的系统调用来完成您需要的一切。

无论如何,这都不是“官方”Perl 6 方式,一旦IO::Socket::Async可以做你想做的事,就可以将所有这些从你的大脑中清除,但以下是如何做到这一点NativeCall

服务器udp.pl

use NativeCall;

constant \AF_INET := 2;
constant \SOCK_DGRAM := 2;

class sockaddr_in is repr('CStruct')
{
    has int16 $.sin_family;
    has uint16 $.sin_port;
    has int32 $.sin_addr;
    has int64 $.pad;
}

sub socket(int32, int32, int32 --> int32) is native() {}
sub bind(int32, sockaddr_in, uint32 --> int32) is native() {}
sub htons(uint16 --> uint16) is native() {}
sub ntohs(uint16 --> uint16) is native() {}
sub inet_ntoa(int32 --> Str) is native() {}
sub perror(Str) is native() {}
sub recvfrom(int32, Blob, size_t, int32, sockaddr_in, int32 is rw --> ssize_t) is native() {}
sub sendto(int32, Blob, size_t, int32, sockaddr_in, int32 --> ssize_t) is native() {}

my int32 $sock = socket(AF_INET, SOCK_DGRAM, 0);
perror('socket') // die if $sock < 0;

my $addr = sockaddr_in.new(sin_family => AF_INET,
                           sin_port => htons(3333),
                           sin_addr => 0);

my $ret = bind($sock, $addr, nativesizeof(sockaddr_in));

perror('bind') // die if $ret < 0;

my $buf = buf8.allocate(1024);

my $fromaddr = sockaddr_in.new;

my int32 $addrsize = nativesizeof(sockaddr_in);

loop
{
    $ret = recvfrom($sock, $buf, $buf.bytes, 0, $fromaddr, $addrsize);
    perror('recvfrom') // die if $ret < 0;

    my $msg = $buf.decode;
    $msg.print;

    my $return-msg = "Thank you for saying $msg";
    my $return-buf = $return-msg.encode;

    $ret = sendto($sock, $return-buf, $return-buf.bytes, 0, $fromaddr, $addrsize);
    perror('sendto') // die if $ret < 0;
}
Run Code Online (Sandbox Code Playgroud)

客户端udp.pl

use NativeCall;

constant \AF_INET := 2;
constant \SOCK_DGRAM := 2;

class sockaddr_in is repr('CStruct')
{
    has int16 $.sin_family;
    has uint16 $.sin_port;
    has int32 $.sin_addr;
    has int64 $.pad;
}

sub socket(int32, int32, int32 --> int32) is native() {}
sub htons(uint16 --> uint16) is native() {}
sub inet_ntoa(int32 --> Str) is native() {}
sub inet_aton(Str, int32 is rw --> int32) is native() {}
sub perror(Str) is native() {}
sub recvfrom(int32, Blob, size_t, int32, sockaddr_in, int32 is rw --> ssize_t) is native() {}
sub recv(int32, Blob, size_t, int32 --> ssize_t) is native() {}
sub sendto(int32, Blob, size_t, int32, sockaddr_in, int32 --> ssize_t) is native() {}

my int32 $sock = socket(AF_INET, SOCK_DGRAM, 0);
perror('socket') // die if $sock < 0;

my int32 $addr-ip;
inet_aton('127.0.0.1', $addr-ip) or die "Bad address";

my $addr = sockaddr_in.new(sin_family => AF_INET,
                           sin_port => htons(3333),
                           sin_addr => $addr-ip);

my $msg = "Hello, Perl 6!\n".encode;

my $ret = sendto($sock, $msg, $msg.bytes, 0, $addr, nativesizeof(sockaddr_in));
perror('sendto') // die if $ret < 0;

my $buf = buf8.allocate(1024);

$ret = recv($sock, $buf, $buf.bytes, 0);

say "Return Msg: ", $buf.decode;
Run Code Online (Sandbox Code Playgroud)