Multicast Server and Client in Java

Submitted by Karthikeyan on

Multicast Server


import java.io.*;
import java.net.*;

public class MulticastServer {
  public static void main(String[] args) throws IOException {
    MulticastSocket socket = new MulticastSocket(1115);
    InetAddress group = InetAddress.getByName("224.0.0.1");
    socket.joinGroup(group);
    while ( true ) {
      byte[] buf = new byte[256];
      DatagramPacket p = new DatagramPacket(buf, buf.length);
      socket.receive(p);
      String s = new String(p.getData(), 0, p.getLength());
      System.out.println(p.getAddress().getHostName() + ": " + s);
    }
  }
}

Multicast Client


import java.io.*;
import java.net.*;

public class MulticastClient {
  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    byte[]      buf  = (br.readLine()).getBytes();
    InetAddress addr = InetAddress.getByName("224.0.0.1");
    DatagramPacket p = 
                 new DatagramPacket(buf, buf.length, addr, 1115);
    MulticastSocket socket = new MulticastSocket();
    socket.send(p);
    socket.close();
  }
}