How to get the actual sockaddr bound by `Unix.(bind (INET_ADDR (inet_addr_any, 0)))`?

lwt
unix

#1

How to get the actual sockaddr bound by Unix.(bind (INET_ADDR (inet_addr_any, 0))) ?

Or Lwt_io.establish_server?

I want to let system to choose a unused port, and get the port number to tell the client to connect.


#2

Do you mean you want to use Unix.listen?

If you bind, you have to specify the port as far as I know.

Take a look at http://www.masterraghu.com/subjects/np/introduction/unix_network_programming_v1.3/ch04lev1sec4.html


#3

I found it: Unix.getsockname


#4

With Lwt_io.establish_server, I believe you currently have to create your own socket, and pass it in with Lwt_io.establish_server ~fd:my_fd .... You can then call Unix.getsockname my_fd.