Domanda

Hi I am trying out java multicast.

I have a WIFI router at - 10.0.0.1 (gateway)

and two nodes:

Node_1 - 10.0.0.4 Node_2 - 10.0.0.3

My IP Multicast sender looks like:

private static class Sender extends Thread
{
    // Create the socket but we don't bind it as we are only going to send data
    private MulticastSocket s;
    private static int senderPort = 15000;
    private static String group = "225.4.5.6";

    public Sender() throws IOException
    {
        s = new MulticastSocket(senderPort);
        s.setInterface(InetAddress.getLocalHost());
        s.joinGroup(InetAddress.getByName(group));
    }

    @Override
    public void run() {
        Integer data = 1; 
        while(true)
        {
            try {
                s.send(new DatagramPacket(data.toString().getBytes(), data.toString().getBytes().length, InetAddress.getByName(group), senderPort));
                Thread.sleep(3000);
                data++;
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                System.out.println("Sender - UnknownHostException");
            }catch (IOException e) {
                // TODO Auto-generated catch block
                System.out.println("Sender - IOException");
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
}

And my IP Multicast receiver looks like:

private static class Receiver extends Thread
{
    private MulticastSocket s;
    private static int receiverPort = 15000;
    private static String group = "225.4.5.6";

    public Receiver() throws IOException
    {
        s = new MulticastSocket(receiverPort);
        s.setInterface(InetAddress.getLocalHost());
        s.joinGroup(InetAddress.getByName(group));

    }

    @Override
    public void run() {

        while (true)
        {
            byte buf[] = new byte[1024];
            DatagramPacket pack = new DatagramPacket(buf, buf.length);
            try {
                System.out.println("Receiver waiting for data");
                s.receive(pack);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            System.out.write(pack.getData(),0,pack.getLength());
            System.out.println();
        }
    }
}

When I have both the Sender and Receiver in the same Node it WORKS but when I have them in different Nodes it does NOT WORK.

What is it that I am missing here??

È stato utile?

Soluzione

By calling setInterface() to the local host you are preventing the joinGroup() message from leaving the current host. That doesn't matter in the sender, because a sender doesn't have to join the group anyway, but in the receiver it will prevent other hosts, routers, etc. from knowing that the receiving host is in the group.

Just remove it.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top