0

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)

Ashutosh Baheti
  • 410
  • 4
  • 20

1 Answers1

0
socket.setNetworkInterface(...);

Get rid of this line. You're setting the interface to the group address, which doesn't make sense. I'm surprised it didn't throw an exception.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Even without that line it was not working... I searched the problem for a while and people said to add networkInterface to the socket... but still it is not working – Ashutosh Baheti Apr 06 '15 at 07:57
  • So the proxy is the problem, or rather another symptom of it. If the two machines aren't in the same subnet, you're depending on the router(s) to forward multicasts. – user207421 Apr 06 '15 at 08:26
  • The machines are in the same subnet. What should I do then? Is there some work around or something else that I'm missing. – Ashutosh Baheti Apr 06 '15 at 13:16