简体   繁体   中英

How to know port assigned to DatagramSocket by OS

I can initialize a DatagramSocket with a zero arg constructor, and the OS will choose the port number for the socket

DatagramSocket socket = new DatagramSocket();

But when I call getPort() on the socket, the result returned is -1

So I'm just wondering, is it even possible to know the port number assigned to the socket by the OS?

You can get it via getLocalPort() .

Rather a trivial question.

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