I’m trying to write some simple program that involves communication with UDP sockets. I would like not to manually bind the port to the socket, and let the OS choose it. It can be achieved by binding to port 0. However, I do not see a way to retrieve the port number from the UDPSocket struct. Function getsockname that can do it is exclusively for TCPSocket.
using Sockets
socket = UDPSocket()
bind(socket, ip"0.0.0.0", 0)
getsockname(socket) # It only works for TCP sockets
close(socket)
Is there any proper way to retrieve the port? I only have hacky approaches, that involves sending one packet to second socket, but that’s just bad.
Looks like the wrapper for uv_udp_getsockname is missing, the TCP one is here. As a (possibly worse than what you are doing) hack, you can call it directly: