Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 402
jjmerelo Avatar asked Mar 03 '19 08:03

jjmerelo


1 Answers

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;
like image 196
Scimon Proctor Avatar answered Sep 20 '22 05:09

Scimon Proctor