Tuesday, November 3, 2015

Filled Under:

Java program to implement multicast

Share
Source (multisource.java)
import java.io.*;
import java.net.*;
public class multisource
{
public static void main(String args[])
{
try
{
DatagramSocket s=new DatagramSocket();
byte[] line=new byte[100];
System.out.println("Enter text to send: ");
int len=System.in.read(line);
InetAddress dest=InetAddress.getByName("224.0.0.1");
DatagramPacket pkt=new DatagramPacket(line,len,dest,16900);
s.send(pkt);
s.close();
}
catch(Exception e)
{
}
}
}

Destination (multidestination.java)
import java.io.*;
import java.net.*;
public class multidestination
{
public static void main(String args[])
{
try
{
MulticastSocket ms=new MulticastSocket(16900);
ms.joinGroup(InetAddress.getByName("224.0.0.1"));
String msg;
do
{
byte[] line=new byte[100];
DatagramPacket pkt=new DatagramPacket(line,line.length);
ms.receive(pkt);
msg=new String(pkt.getData());
System.out.println("Message from "+pkt.getAddress()+": "+msg.trim());
}
while(!msg.trim().equals("close"));
ms.close();
}
catch(Exception e)
{
}
}
}

0 comments:

Post a Comment