Multicast in java

狂风中的少年 提交于 2020-01-13 04:41:06

问题


I am trying to write a simple multicast trial.

I used a standard code (sender and reciever).

I tried a few different standard pieces of code. it appears that the receiving code is stuck on receive (as if it's not receving anything).

receive side:

        byte[] b = new byte[3];
    DatagramPacket dgram = new DatagramPacket(b, b.length);
    MulticastSocket socket =
      new MulticastSocket(4545); // must bind receive side
    socket.joinGroup(InetAddress.getByName("226.100.100.125"));

    while(true) {
      socket.receive(dgram); // blocks until a datagram is received
      System.err.println("Received " + dgram.getLength() +
        " bytes from " + dgram.getAddress());
      dgram.setLength(b.length); // must reset length field!
    }

sending side:

      DatagramSocket socket = new DatagramSocket();

  byte[] b = new byte[]{(byte)1,(byte)5,(byte)3};
  DatagramPacket dgram;

  dgram = new DatagramPacket(b, b.length,
    InetAddress.getByName("226.100.100.125"), 4545);

  System.err.println("Sending " + b.length + " bytes to " +
    dgram.getAddress() + ':' + dgram.getPort());
  while(true) {
    System.err.print(".");
    socket.send(dgram);
    Thread.sleep(1000);
  }

What is wrong with my code? *I tried alot of different IPs also*

thanks for the help.


回答1:


Try receiving in from the same IP but sending to localhost. If this works, then it's your router that is the problem as it doesn't support multicasting. If this still doesn't work then it's the IP address. try something in the 233.x.x.x - 239.x.x.x range.

I ran your code on my computer and it works fine as is, and also works if I changed the send address to localhost. Sounds like this is a problem with your router and not your code.




回答2:


The network 239.0.0.0/8 is designated for administrator multi-cast traffic. If all of your machines are on the same network segment, you can use an ip in this network to play around with multi-casting.

Here is the RFC defining these:

http://tools.ietf.org/html/rfc2365

As far as sending goes... Your code should look something like this:

            DatagramPacket p = ...
            MulticastSocket s = new MulticastSocket(LISTENPORT);
            InetAddress group = InetAddress.getByName(LISTENIP);
            s.joinGroup(group);
            s.send(p);
            s.leaveGroup(group);



回答3:


Multicast usually (in a real network) depends on the router's support. From what i know in general you can't really count on it being supported properly. I would try to send packets from a different client (command line or something else) to see if the server side is binding properly or not.

On the other side if you look here: http://www.iana.org/assignments/multicast-addresses/multicast-addresses.xml it sais that the 226 address block is marked as reserved. The referenced RFC sais:

  1. Use of IANA Reserved Addresses

    Applications MUST NOT use addressing in the IANA reserved blocks.

That might have something to do with it also.




回答4:


You shall not use same port, try different port and create one socket for receiving and second one for sending.




回答5:


When this happens on my Linux boxes I check to make sure that

1) there is a route for 224.0.0.0/4 on the correct interfaces 2) the source IP address matches one of the routes for that interface

#2 is the stickiest in my lab. If my eth1 only has a route for 10.77.4.0/24 and some box is transmitting from 10.78.5.15, then linux discards it as a "martian packet".



来源:https://stackoverflow.com/questions/5072028/multicast-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!