Networked VR Lab Update

The Week 8 lab notes describe how to create sockets for UDP broadcasting in Java by using IP addresses ending in .255. Using broadcast addresses is extremely unfriendly to anyone else on the same LAN, and in many parts of the Internet will cause the local network SWAT team to visit you. Use multicasting instead.

Multicast addresses are (for IP4) range 224.x.x.x to 239.x.x.x. Those beginning with 239. are reserved for use within private domains, such as the eScience lab.

A Java MulticastSocket is a subclass of DatagramSocket, with the same behaviour and methods for sending or receiving packets.

To create a multicast socket:


  String addr = [ip address];
  InetAddress dest = InetAddress.getByName(addr);
  int port = [your port number];
  MulticastSocket s  = new MulticastSocket(port);
  s.joinGroup(dest);

The ip address would be something like "239.0.0.8". Any packet sent to this address should be received by any other host that has joined the same address.

To create a DatagramSocket that can be used either for unicasting (one to one) or multicasting:


String addr = [ip address];
InetAddress dest = InetAddress.getByName(addr);
int port = [your port number];
if (dest.isMulticastAddress()) {
  DatagramSocket s  = new MulticastSocket(port);
  ((MulticastSocket)s).joinGroup(dest);
} else
  DatagramSocket s = new DatagramSocket(port);

Although more complex, this is useful because hosts cannot always multicast to themselves. If you want to test your networking code on a single host, you will have to use the unicast IP address. An easy way to do this is to pass the IP address as a command line argument.


Written by Hugh Fisher