问题
Referring to the Java 6 API docs for the DatagramSocket class:
UDP broadcasts sends are always enabled on a DatagramSocket. In order to receive broadcast packets a DatagramSocket should be bound to the wildcard address. In some implementations, broadcast packets may also be received when a DatagramSocket is bound to a more specific address.
Could someone tell me what the 'wildcard address' is? And is the following valid for listening for UDP broadcasts:
MulticastSocket socket = new MulticastSocket(new InetSocketAddress(InetAddress.getByName("0.0.0.0"),4445);
回答1:
The wildcard address is 0.0.0.0.
Not to be confused with the broadcast-to-all-subnets address, which is 255.255.255.255.
It is more correctly called the 'any' address, after INADDR_ANY.
In Java it is most easily used by supplying null
as a bind-address, or omitting the parameter altogether, e.g. new InetSocketAddress(null, 0)
or new InetSocketAddress(0)
respectively. In other words it is the default when binding, and therefore implicitly 'good practice'.
来源:https://stackoverflow.com/questions/14733183/what-is-the-wildcard-address-in-the-context-of-udp-broadcasts