Is there a way to bind a DatagramSocket to any port on a specific nic, or if I specify nic, should I specify a port?

Is this just not possible in Java? The DatagramSocket.bind method must accept either NULL or IP: PORT. You cannot just pass the IP address describing the network adapter and let PORT be automatically selected. The view does not make sense if I am missing something.

+4
source share
2 answers

Use the default constructor of DatagramSocket . This will be bound to any port. You do not need to use the bind method.

+1
source

Simply enter a port number of zero, or omit it, as suggested by Mike Q. In both cases, the system will allocate a port for you.

+1
source

Source: https://habr.com/ru/post/1434061/


All Articles