MulticastSocket不接收已发送的数据包

时间:2016-05-18 16:30:11

标签: java multicastsocket

我试图在两个进程之间使用MulticastSocket。 服务器发送数据包和客户端需要读取它。服务器代码是:

MulticastSocket multicastSocket = new MulticastSocket();
multicastSocket.setTimeToLive((Integer) config.getValue("MULTICAST_TTL"));
multicastSocket.setLoopbackMode(false);
multicastSocket.setReuseAddress(true);

String msg = "KA";
InetAddress multicastGroup = "225.3.0.1";
int port = 4000;
DatagramPacket pkt = new DatagramPacket(msg.getBytes(), msg.getBytes().length, multicastGroup, port);


while(true) {
     try{
        multicastSocket.send(pkt);
        System.out.println("SPEDITO PACCHETTO: "+pkt.getSocketAddress());
     }catch ( IOException e){
        System.out.println("Errore di comunicazione con la rete multicast. "+e.getMessage());
     }
}

客户端代码是:

  this.multicastSocket = new MulticastSocket(4000);
  InetAddress multicastGroup = InetAddress.getByName("225.3.0.1");
  this.multicastSocket.joinGroup(multicastGroup);
  DatagramPacket pkt = new DatagramPacket(new byte[512], 512);
    while(true){
            System.out.println("GOING TO READ");
            multicastSocket.receive(pkt);
            System.out.println("READ");
            byte[] b = pkt.getData();
            String msg = new String(b, 0, pkt.getLength());
    }

问题是服务器发送数据包(send(pkt)返回),而不是客户端没有收到packate(接收(pkt)不返回)。问题在哪里?

p.s。:服务器和客户端在同一台计算机上,MULTICAST_TTL为1。

0 个答案:

没有答案