J.E.D.I.
10.2.2 The MulticastSocket and the DatagramPacket Class
The MulticastSocket class is useful for applications that implement group communication. IP addresses for a multicast group lies within the range 224.0.0.0 to 239.255.255.255.
However, the address 224.0.0.0 is reserved and should not be used. This class has three constructors but well just consider one of these constructors.
MulticastSocket Constructors
MulticastSocketint port Creates a multicast socket bound to the given port number.
Table 38: MulticastSocket constructor
The following table gives the description of some MulticastSocket methods.
MulticastSocket Methods
public void joinGroupInetAddress mcastaddr Join a multicast group on the specified address.
public void leaveGroupInetAddress mcastaddr Leave a multicast group on the specified address.
public void sendDatagramPacket p An inherited method from the DatagramSocket class. Sends p from this socket.
Table 39: MulticastSocket methods
Before one can send a message to a group, one should first be a member of the multicast group by using the joinGroup method. A member can now send messages
through the send method. Once youre done talking with the group, you can use the leaveGroup method to relinquish your membership.
Before looking at an example of using the MulticastSocket class, lets first have a quick look at the DatagramPacket class. Observe that in the send method of the
Introduction to Programming II Page 145
J.E.D.I.
MulticastSocket class, the required parameter is a DatagramPacket object. Thus, we need to understand this type of objects before using the send method.
The DatagramPacket class is used to deliver data through a connectionless protocol such as a multicast. A problem with this is that the delivery of packets is not guaranteed. Let
us now consider two of its six constructors.
DatagramPacket Constructors
DatagramPacketbyte[] buf, int length Constructs a datagram packet for receiving packets with a length length. length should
be less than or equal to the size of the buffer buf. DatagramPacketbyte[] buf, int length, InetAddress address, int port
Constructs a datagram packet for sending packets with a length length to the specified port number on the specified host.
Table 40: DatagramPacket constructors
Here are some interesting methods of the DatagramPacket class.
DatagramPacket Methods
public byte[] getData Returns the buffer in which data has been stored.
public InetAddress getAddress Returns the IP address of the machine where the packet is being sent to or was
received from. public int getLength
Returns the length of data being sent or received. public int getPort
Returns the port number on the remote host where the packet is being sent to or was received from.
Table 41: DatagramPacket methods
Our multicast example also consists of two classes, a server and a client. The server receives messages from the client and prints out these messages.
Here is the server class.
import java.net.; public class ChatServer {
public static void mainString args[] throws Exception { MulticastSocket server = new MulticastSocket1234;
InetAddress group = InetAddress.getByName234.5.6.7; getByName - returns IP address of the given host
server.joinGroupgroup; boolean infinite = true;
Server continually receives data and prints them whileinfinite {
byte buf[] = new byte[1024]; DatagramPacket data = new DatagramPacketbuf,
buf.length; server.receivedata;
Introduction to Programming II Page 146
J.E.D.I.
String msg = new Stringdata.getData.trim; System.out.printlnmsg;
} server.close;
} }
Introduction to Programming II Page 147
J.E.D.I.
Here is the client class. import java.net.;
import java.io.; public class ChatClient {
public static void mainString args[] throws Exception { MulticastSocket chat = new MulticastSocket1234;
InetAddress group = InetAddress.getByName234.5.6.7; chat.joinGroupgroup;
String msg = ; System.out.printlnType a message for the server:;
BufferedReader br = new BufferedReadernew InputStreamReaderSystem.in;
msg = br.readLine; DatagramPacket data = new DatagramPacketmsg.getBytes,
0, msg.length, group, 1234; chat.senddata;
chat.close; }
}
Here is a sample run of ChatServer and ChatClient class, assuming that ChatServer was executed before running the client class:
Executing ChatServer makes it ready to accept messages from clients
Running ChatClient – simply passes message to the server and then terminates
Type a message for the server: first chat message to server
ChatServer receives message from client and prints this message
first chat message to server
Running ChatClient again Type a message for the server:
second chat message to server
ChatServer receives message from client and prints this message
second chat message to server
Introduction to Programming II Page 148
J.E.D.I.
11 Applets
11.1 Objectives