java DatagramSocket接收数据 组播Socket发送数据

发布于 2024-10-06 14:06:26 字数 66 浏览 2 评论 0 原文

任何人都可以向我展示一个 java 示例,用于从 DatagramSocket 接收数据并通过多播套接字发送相同的数据

can anyone show me an example in java to receive data from DatagramSocket and sending same data through Multicast Socket

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

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

发布评论

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

评论(2

ゝ杯具 2024-10-13 14:06:26

发送多播数据报

为了在 Java 中发送任何类型的数据报,无论是单播、广播还是多播,都需要一个 java.net.DatagramSocket

DatagramSocket socket = new DatagramSocket();

可以选择提供一个套接字必须绑定到的 DatagramSocket 构造函数的本地端口。仅当需要其他方能够在特定港口联系我们时才需要这样做。第三个构造函数采用本地端口和要绑定的本地 IP 地址。这(很少)用于多宿主主机,在多宿主主机上接收流量的网络适配器很重要。

 DatagramSocket socket = new DatagramSocket();

byte[] b = new byte[DGRAM_LENGTH];
DatagramPacket dgram;

dgram = new DatagramPacket(b, b.length,
  InetAddress.getByName(MCAST_ADDR), DEST_PORT);

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

接收多播数据报

人们可以使用普通的 DatagramSocket 来发送和接收单播和广播数据报以及发送多播数据报。然而,为了接收多播数据报,需要一个 MulticastSocket。原因很简单,UDP 以下的所有协议层都需要做额外的工作来控制和接收多播流量。

byte[] b = new byte[BUFFER_LENGTH];
DatagramPacket dgram = new DatagramPacket(b, b.length);
MulticastSocket socket =
  new MulticastSocket(DEST_PORT); // must bind receive side
socket.joinGroup(InetAddress.getByName(MCAST_ADDR));

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 multicast datagrams

In order to send any kind of datagram in Java, be it unicast, broadcast or multicast, one needs a java.net.DatagramSocket:

DatagramSocket socket = new DatagramSocket();

One can optionally supply a local port to the DatagramSocket constructor to which the socket must bind. This is only necessary if one needs other parties to be able to reach us at a specific port. A third constructor takes the local port AND the local IP address to which to bind. This is used (rarely) with multi-homed hosts where it is important on which network adapter the traffic is received.

 DatagramSocket socket = new DatagramSocket();

byte[] b = new byte[DGRAM_LENGTH];
DatagramPacket dgram;

dgram = new DatagramPacket(b, b.length,
  InetAddress.getByName(MCAST_ADDR), DEST_PORT);

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

Receiving multicast datagrams

One can use a normal DatagramSocket to send and receive unicast and broadcast datagrams and to send multicast datagrams. In order to receive multicast datagrams, however, one needs a MulticastSocket. The reason for this is simple, additional work needs to be done to control and receive multicast traffic by all the protocol layers below UDP.

byte[] b = new byte[BUFFER_LENGTH];
DatagramPacket dgram = new DatagramPacket(b, b.length);
MulticastSocket socket =
  new MulticastSocket(DEST_PORT); // must bind receive side
socket.joinGroup(InetAddress.getByName(MCAST_ADDR));

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

For more Information:

遇见了你 2024-10-13 14:06:26

你已经把它从后到前了。您通过 MulticastSocket接收多播,但不需要以这种方式发送它们:您可以通过 DatagramSocket 发送它们。

请参阅 Java 教程,自定义网络跟踪

You've got that back to front. You receive multicasts through a MulticastSocket, but you don't need to send them that way: you can send them via a DatagramSocket.

See the Java Tutorial, Custom Networking trail.

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