Java多播发送数据,未接收 [英] Java Multicast Sending Data, Not Receiving

查看:103
本文介绍了Java多播发送数据,未接收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java编写一个用于大大简化多播过程的类。但是,我遇到两个大问题:

I am writing a class in Java which is used to greatly simplify the process of multicasting. However, I am having two big problems with it:


  1. 该类发送数据(我可以使用我的网络监视器Wireshark验证这一点)但是同一组中的任何其他人都不会收到数据。

  2. 在某些机器上,传输过程中超出了发送数据包TTL(同样,根据Wireshark的说法)。

有人可以帮帮我吗?我一直在努力寻找几个小时的答案,看来我的代码遵循了从多播主机连接,加入,发送和接收数据的所有基本过程。

Could anyone please help me? I've been trying and searching for answers for hours, and it appears that my code follows all of the basic procedures for connecting to, joining, sending, and receiving data from a multicast host.

以下是该课程相关部分的片段:

Here is a snippet of the relevant portions of the class:

复式课程:

public class Multicaster {
  public int port = 5540;

  protected String IPAddress;

  private MulticastSocket msConn;
  private InetAddress netAddr;

  public Multicaster(String IPAddress) {
    this.IPAddress = IPAddress;
  }

  public String recieveData() {
    byte[] buf = new byte[1000];
    DatagramPacket pack = new DatagramPacket(buf, buf.length);

    try {
      this.msConn.receive(pack);
      new Message(pack);
      String out = new String(pack.getData());
      return out.substring(0, pack.getLength());
    } catch (IOException e) {
      return new String("");
    }
  }

  public void joinGroup() { 
    try {
      this.msConn.joinGroup(this.netAddr);
    } catch (IOException e) {
    //This error shouldn't occur since it would caught by the connect() method during initial connection to the host
    }
  }

  public void connect() throws MulticasterInitException {
  //Try to create a multicast connection on the given IP address and port
    try {
      try {
      //Create a multicast connection on a given port, throws UnknownHostException
        this.msConn = new MulticastSocket(this.port);

      //If all goes well, then create a connection to a given IP address using the above port number, throws IOException and SecurityException
        this.netAddr = InetAddress.getByName(this.IPAddress);
      }
    }

  /**
   * Here all of the possible exceptions that are thrown above
   * are caught and handled here. This works just fine.
   */
  }

  public void sendData(String data) throws MulticasterSendException {
    DatagramPacket packet = new DatagramPacket(data.getBytes(), data.length(), this.netAddr, this.port);

    try {
      this.msConn.send(packet);
    } catch (IOException e) {
      throw new MulticasterSendException("Java could not communicate with the server. Please check your network connections.", e);
    }
  }
}

示例用法发送数据:

Multicaster multicast = new Multicaster("239.0.0.0");

try {
  multicast.connect();
} catch (MulticasterInitException e) {
  //Handle exception...
}

multicast.joinGroup();

try {
  multicast.sendData("Hi");
} catch (MulticasterSendException e) {
  //Handle exception... 
}

接收数据的示例用法:

Multicaster multicast = new Multicaster("239.0.0.0");

try {
  multicast.connect();
} catch (MulticasterInitException e) {
  //Handle exception...
}

multicast.joinGroup();
System.out.print(multicast.recieveData());


推荐答案

我之前遇到过类似的问题,不得不确保在接收方指定了NetworkInterface。

I've run into similar problems before and had to ensure that the NetworkInterface was specified on the receiving side.

SocketAddress socketAddress = new SocketAddress(groupIp, groupPort);
NetworkInterface networkInterface = NetworkInterface.getByName(interfaceName);

socket.joinGroup(socketAddress, networkInterface);

其中 interfaceName 是其中一个接口名称在Linux或Mac上运行 ifconfig 时显示。

Where interfaceName is one of the interface names shown when running ifconfig on Linux or Mac.

这篇关于Java多播发送数据,未接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆