Experiment No.
7
Introduction of Multicast socket Programming in Java Application
Multicasting in Java
1. Introduction
This article deals primarily with the subject of multicast communication in Java.
2. Sending multicast datagrams
In order to send any kind of datagram in Java, one needs a
java.net.DatagramSocket:
DatagramSocket socket = new DatagramSocket();
This sample code creates the socket and a datagram to send and then simply sends
the
same datagram every second:
DatagramSocket socket = new DatagramSocket();
byte[] b = new byte[DGRAM_LENGTH];
DatagramPacketdgram;
dgram = new DatagramPacket(b, b.length,
InetAddress.getByName(MCAST_ADDR), DEST_PORT);
System.err.println("Sending " + b.length + " bytes to " +
dgram.getAddress() + ':' + dgram.getPort());
while(true) {
System.err.print(".");
socket.send(dgram);
Thread.sleep(1000);
Valid values for the constants are:
•DGRAM_LENGTH: anything from 0 to 65507 (see section 5),
•MCAST_ADDR: any class D address (see appendix D), from 224.0.0.0 to
235.1.1.1,
•DEST_PORT: an unsigned 16-bit integer, eg. 6789 or 7777.
3. Receiving multicast datagrams
One can use a normal DatagramSocket to send and receive multicast datagrams as
seen
in the section 2. In order to receive multicast datagrams, however, one needs a
MulticastSocket. The reason for this is simple, additional work needs to be done to
control and receive multicast traffic by all the protocol layers below UDP.
The example given below, opens a multicast socket, binds it to a specific port and
joins
a specific multicast group:
byte[] b = new byte[BUFFER_LENGTH];
DatagramPacketdgram = new DatagramPacket(b, b.length);
MulticastSocket socket =
new MulticastSocket(DEST_PORT); // must bind receive side
socket.joinGroup(InetAddress.getByName(MCAST_ADDR));
while(true) {
socket.receive(dgram); // blocks until a datagram is received
System.err.println("Received " + dgram.getLength() +
" bytes from " + dgram.getAddress());
dgram.setLength(b.length); // must reset length field!
}
Experiment No. 8
A Program toimplement Multicast Socket Program in Java
1 Objective
• Learning Basics of Multicast Socket And Datagram Packet classes
of java.net packages
• Designing customized messages to send from one Machine or IP to
a group of Machine or IPs
• How to implement Multicast send data through Java Technology.
2 After completing this experiment you will be able to:
• Create Multicasting client arrays and sending IP of the Macj=hine
to communicate
• Learning basics of Java Multicast Socket and Datagram Packet’s
Data structure
• Understanding basics of some intermediate steps required to get
communicated from one pc to array of PC sousing Java
Applications.
3 Requirements: - Tools / Apparatus: Unix/Linux Operating System, Bash
Shell, OpenJDK and JRE installed on Machines
4 Solution
/*JAVA CODE */
import java.io.*;
public class QuoteServer {
public static void main(String[] args) throws IOException {
new QuoteServerThread().start();
}
}
The QuoteServerThread Class
The QuoteServerThread class implements the main logic of the quote
server:
import java.io.*;
import java.net.*;
import java.util.*;
public class QuoteServerThread extends Thread {
protected DatagramSocket socket = null;
protected BufferedReader in = null;
protected booleanmoreQuotes = true;
public QuoteServerThread() throws IOException {
this("QuoteServerThread");
}
public QuoteServerThread(String name) throws IOException {
super(name);
socket = new DatagramSocket(4445);
try {
in = new BufferedReader(new FileReader("one-liners.txt"));
} catch (FileNotFoundException e) {
System.err.println("Could not open quote file. Serving time instead.");
}
}
public void run() {
while (moreQuotes) {
try {
byte[] buf = new byte[256];
// receive request
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
// figure out response
String dString = null;
if (in == null)
dString = new Date().toString();
else
dString = getNextQuote();
buf = dString.getBytes();
// send the response to the client at "address" and "port"
InetAddress address = packet.getAddress();
int port = packet.getPort();
packet = new DatagramPacket(buf, buf.length, address, port);
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
moreQuotes = false;
}
}
socket.close();
}
protected String getNextQuote() {
String returnValue = null;
try {
if ((returnValue = in.readLine()) == null) {
in.close();
moreQuotes = false;
returnValue = "No more quotes. Goodbye.";
}
} catch (IOException e) {
returnValue = "IOException occurred in server.";
}
return returnValue;
}
}
When created, the QuoteServerThread creates a DatagramSocket on port
4445 (arbitrarily chosen). This is the DatagramSocket through which the
server communicates with all of its clients.
public QuoteServerThread() throws IOException {
this("QuoteServer");
}
public QuoteServerThread(String name) throws IOException {
super(name);
socket = new DatagramSocket(4445);
try {
in = new BufferedReader(new FileReader("one-liners.txt"));
} catch (FileNotFoundException e)
System.err.println("Couldn't open quote file. " +"Serving time instead.");
}
}