1

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);
D-Dᴙum
  • 7,689
  • 8
  • 58
  • 97
  • Here http://www.rhyshaden.com/ipadd.htm is a whole section on Wildcards :) For listening to broadcasts a wildcard-bound DatagramSocket should be enough. – Fildor Feb 06 '13 at 15:59
  • 1
    See http://stackoverflow.com/questions/5472269/java-datagramsocket-listening-on-a-broadcast-address – Carlo Pellegrini Feb 06 '13 at 16:03
  • Carlo, that is indeed what I based my above example upon but I wondered if using 0.0.0.0 is 'good practice'.. – D-Dᴙum Feb 06 '13 at 16:07
  • 1
    Well, it's the only practice doing the job of listening to broadcasts from any sender. – Fildor Feb 06 '13 at 17:28

1 Answers1

3

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'.

user207421
  • 305,947
  • 44
  • 307
  • 483