Java多播发送数据,而不是接收数据

发布于 2024-12-20 18:10:21 字数 2826 浏览 1 评论 0原文

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

  1. 该类发送数据(我可以使用我的网络监视器 Wireshark 验证这一点),但同一组中的任何其他人都不会收到该数据。
  2. 在某些机器上,发送数据包在传输过程中会超出 TTL(同样,根据 Wireshark 的说法)。

有人可以帮我吗?我已经尝试和寻找答案几个小时了,看来我的代码遵循连接、加入、发送和接收来自多播主机的数据的所有基本过程。

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

Multicaster 类:

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());

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. The class sends data (I can verify this with my net monitor, Wireshark) but the data is not received by any others in the same group.
  2. On some machines, the sending packet TTL is exceeded in transit (again, according to 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:

Multicaster 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);
    }
  }
}

Sample usage to send data:

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... 
}

Sample usage to receive data:

Multicaster multicast = new Multicaster("239.0.0.0");

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许你一世情深 2024-12-27 18:10:21

我之前遇到过类似的问题,并且必须确保在接收端指定了 NetworkInterface。

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

socket.joinGroup(socketAddress, networkInterface);

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

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

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

socket.joinGroup(socketAddress, networkInterface);

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文