如何在同一台机器上用 Java 测试广播 udp 数据包?数据包、测试、在同一、机器上

2023-09-08 09:12:02 作者:画船听雨眠

我可以在我的机器上执行 UDP 广播数据包吗?我没有网络,我只有便宜的 linux 盒子.我想让一个服务器广播一个数据包,而两个或多个客户端在同一台机器上接收它们.那可能吗?我使用什么 IP?

Can I perform a UDP broadcast packets on my machine? I don't have a network, i just have my cheap linux box. I want to have a server broadcasting a packet and two or more clients in the same machine receiving them. Is that possible? What IP do I use?

@gravyface 给了我希望,但我尝试了:1)服务器发送到 127.255.255.255:54321,客户端监听 0.0.0.0:54321.2) 服务器发送到 127.255.255.255:54321 和客户端监听 127.0.0.1:54321.3) 服务器发送到 127.255.255.255:54321 和客户端监听 127.255.255.255:54321.它们都不起作用! :(

@gravyface gave me hope but I tried: 1) server sending to 127.255.255.255:54321 and clients listenting to 0.0.0.0:54321. 2) server sending to 127.255.255.255:54321 and clients listening to 127.0.0.1:54321. 3) server sending to 127.255.255.255:54321 and clients listening to 127.255.255.255:54321. None of them worked! :(

OBS:我正在使用 REUSE_ADDR 和 SO_BROADCAST 选项.

OBS: I am using REUSE_ADDR and SO_BROADCAST options.

推荐答案

我可以确认 127.255.255.255 在 Linux (Ubuntu) 上工作,在 Mac (Snow Leopard) 上不工作.如果您知道如何在 Mac 上执行此操作,请告诉我.:)

I am able to confirm that 127.255.255.255 works on Linux (Ubuntu) and it does not work on Mac (Snow Leopard). If you ever find out how to do that on Mac let me know. :)