gpt4 book ai didi

Java:无法接收多播数据

转载 作者:搜寻专家 更新时间:2023-10-31 20:05:47 26 4
gpt4 key购买 nike

我正在尝试测试多播客户端服务器应用程序,但我的客户端无法接收任何数据。我通读了教程,但找不到任何方法。你能帮帮我吗?

客户端代码

MulticastSocket socket = new MulticastSocket("9000");
socket.setInterface(InetAddress.getLocalHost());
socket.joinGroup("237.0.0.1");

while(true)
{
byte ab[] = new byte[100];
DatagramPacket packet = new DatagramPacket(ab, ab.length);
socket.receive(packet);
System.out.println("Got packet " + Arrays.toString(ab));
}

服务器代码

MulticastSocket socket = new MulticastSocket("9000");
socket.setInterface(InetAddress.getLocalHost());

socket.joinGroup("237.0.0.1");

byte index = 0;
while(true)
{
byte[] bt = new byte[100];
Arrays.fill(bt, (byte)index++);
DatagramPacket packet = new DatagramPacket(bt, 100,"237.0.0.1", "9000");
socket.send(packet);
System.out.println("sent 100 bytes");
Thread.sleep(10*1000);
}

我认为问题出在我设置界面的方式上。

有人可以帮我解决这个问题,弄清楚我想了解的内容吗。

最佳答案

您的代码无法在我的系统上编译,但是当我进行以下更改(如下)时,它会按预期工作:

  • 端口号应该是int

    MulticastSocket socket = new MulticastSocket(9000);
  • IP 地址应指定为 InetAddress,使用:

    InetAddress.getByName("237.0.0.1")

除此之外,请检查您的防火墙是否阻止了流量


完整示例(带有上面的编译修复):

public static void main(String[] args) throws Exception {

final InetAddress group = InetAddress.getByName("237.0.0.1");
final int port = 9000;

new Thread(new Runnable() {
@Override
public void run() {
try {
MulticastSocket socket = new MulticastSocket(port);
socket.setInterface(InetAddress.getLocalHost());
socket.joinGroup(group);

DatagramPacket packet = new DatagramPacket(new byte[100], 100);
while(true) {
socket.receive(packet);
System.out.println("Got packet " +
Arrays.toString(packet.getData()));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();

new Thread(new Runnable() {
@Override
public void run() {
try {
MulticastSocket socket = new MulticastSocket(port);
socket.setInterface(InetAddress.getLocalHost());
socket.joinGroup(group);

byte[] bt = new byte[100];
byte index = 0;
while(true) {
Arrays.fill(bt, (byte) index++);
socket.send(new DatagramPacket(bt, 100, group, port));
System.out.println("sent 100 bytes");
Thread.sleep(1*1000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}

输出:

sent 100 bytes
Got packet [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
sent 100 bytes
Got packet [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
sent 100 bytes
Got packet [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 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 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6]
^C

关于Java:无法接收多播数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7469264/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com