0% found this document useful (0 votes)
162 views3 pages

Multicast Program

This document contains code for a multicast server and client program in Java. The server program sends 5 messages to a multicast group address and port over 5 seconds. The client program joins the multicast group to receive the messages sent by the server and prints them out.

Uploaded by

Balayogi G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
162 views3 pages

Multicast Program

This document contains code for a multicast server and client program in Java. The server program sends 5 messages to a multicast group address and port over 5 seconds. The client program joins the multicast group to receive the messages sent by the server and prints them out.

Uploaded by

Balayogi G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

MULTICAST PROGRAM

SERVER:
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class MulticastSocketServer {

final static String INET_ADDR = "224.0.0.3";


final static int PORT = 8888;

public static void main(String[] args) throws UnknownHostException, InterruptedException {


// Get the address that we are going to connect to.
InetAddress addr = InetAddress.getByName(INET_ADDR);

// Open a new DatagramSocket, which will be used to send the data.


try (DatagramSocket serverSocket = new DatagramSocket()) {
for (int i = 0; i < 5; i++) {
String msg = "Sent message no " + i;

// Create a packet that will contain the data


// (in the form of bytes) and send it.
DatagramPacket msgPacket = new DatagramPacket(msg.getBytes(),
msg.getBytes().length, addr, PORT);
serverSocket.send(msgPacket);

System.out.println("Server sent packet with msg: " + msg);


Thread.sleep(500);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

CLIENT:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;

public class MulticastSocketClient {


final static String INET_ADDR = "224.0.0.3";
final static int PORT = 8888;
public static void main(String[] args) throws UnknownHostException {
// Get the address that we are going to connect to.
InetAddress address = InetAddress.getByName(INET_ADDR);
// Create a buffer of bytes, which will be used to store
// the incoming bytes containing the information from the server.
// Since the message is small here, 256 bytes should be enough.
byte[] buf = new byte[256];
// Create a new Multicast socket (that will allow other sockets/programs
// to join it as well.
try (MulticastSocket clientSocket = new MulticastSocket(PORT)){
//Joint the Multicast group.
clientSocket.joinGroup(address);
while (true) {
// Receive the information and print it.
DatagramPacket msgPacket = new DatagramPacket(buf, buf.length);
clientSocket.receive(msgPacket);
String msg = new String(buf, 0, buf.length);
System.out.println("Socket 1 received msg: " + msg);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
OUTPUT:

You might also like