First let me repeat my comment above. From reading the documentation for IO :: Socket :: Async, I see no obvious way to do this. You can either configure the UDP sender or the UDP receiver, but not both.
UDP connections are determined by four things (sender address, sender port, receiver address, receiver port).
The server can listen to this address / port. Once a packet is received, there are usually ways to request the sender address / port. This is what I do not see for Perl 6.
The client can forward the packet to a specific server address / port. The client usually chooses a random "sender port", providing the fourth element necessary for the "connection" (in this protocol, without connection).
So, as in your examples from other languages, the client sends the packet, the server looks at the address / port of the sender, and then returns the packet to the same address / port. The client, having sent its packet, again listens to the same random port to which it sent the packet to receive a response from the server. I don't see an obvious way in Perl 6 to monitor print-to with recv in the same port just sent.
With that said, Perl 6 has a fantastic NativeCall object that you can use to directly invoke dynamic libraries, so you can do whatever you need with the actual system calls if you are so prone.
This is not the βofficialβ Perl-6 method, and as soon as IO::Socket::Async can do what you want, clear all of this from your brain, but here's how to do it with NativeCall :
server-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; }
client-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;