简体   繁体   中英

Ruby get UDPSocket port number

I am trying get the port number after I have bind my UDPSocket to a free port number. so

udp_server = UDPSocket.new

udp_server.bind('localhost', 0)

So I am passing a 0 here to let udp_server randomly pick a free port number for me.

Is it possible to get the port number after doing the bind? Say something like

udp_server.get_port_number

I have searched online but didn't find a solution.

Thanks a lot!

应该这样做:

udp_server.addr[1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM