rakuasyncsocket

Finding out the port an asynchronous socket is binding to?


Since version 6.d of Perl 6, you can use port 0 to ask the interpreter to find a port to bind for you:

my $socket = IO::Socket::Async.listen($SOCKET_ADDR, 0);

However, $socket is a Supply with no information on the low-level socket it's using. What is the way of finding which port is it binding to?


Solution

  • When you tap the $socket variable you get back a (currently undocumented) IO::Socket::Async::ListenSocket object. This has a couple of methods socket-port and socket-host which are Promises then when they resolve have the correct values.

    We can probably tidy up the docs to indicate this.

    Example :

    my $s = IO::Socket::Async.listen("127.0.0.1",0);
    my $t = $s.tap;
    my $p = await $t.socket-port;
    $p.say;