I am trying to implement multicast communication over the internet. Here is my code
First Send then receive 5 times Code:
public static void main(String args[]) throws IOException
{
MulticastSocket socket = new MulticastSocket(4446);
InetAddress group = InetAddress.getByName("228.5.6.7");
socket.joinGroup(group);
socket.setNetworkInterface(NetworkInterface.getByInetAddress(group));
DatagramPacket packet;
System.out.println("Trasmitter Started!!");
for (int i = 0; i < 5; i++)
{
String buf = "Hi receiver";
packet = new DatagramPacket(buf.getBytes(), buf.getBytes().length,group,4446);
socket.send(packet);
System.out.println("Packet Sent!");
byte[] buff = new byte[256];
packet = new DatagramPacket(buff,buff.length);
socket.receive(packet);
String received = new String(packet.getData());
System.out.println("Quote of the Moment: " + received);
}
socket.leaveGroup(group);
socket.close();
}
Only Receive :
public static void main(String args[]) throws IOException
{
System.setProperty("java.net.preferIPv4Stack", "true");
MulticastSocket socket = new MulticastSocket(4446);
InetAddress group = InetAddress.getByName("228.5.6.7");
//socket.setNetworkInterface(NetworkInterface.getByInetAddress(group));
socket.setInterface(group);
socket.joinGroup(group);
DatagramPacket packet;
System.out.println("Receiver Started!!");
for (int i = 0; i < 5; i++)
{
byte[] buff = new byte[256];
packet = new DatagramPacket(buff,buff.length);
socket.receive(packet);
String received = new String(packet.getData());
System.out.println("Received Message: " + received);
}
socket.leaveGroup(group);
socket.close();
}
The Code works on a single machine but when I try to run the receiver on another machine it doesn't receive anything. I am not getting where I'm going wrong? I searched for some solutions and they said to add networkInterface to the socket, but that too didn't work.
Operating System : Windows 8.1
I'm also using a proxy (if that may be the problem)