java udp广播问题

发布于 2024-10-31 05:58:24 字数 1824 浏览 0 评论 0原文

我的 udp 广播行为有问题,

public static final int PORT = 34567;

public static void main(String[] args) throws IOException,
        InterruptedException {
    if (args.length > 0 && args[0].equals("server")) {
        int port;
        if (args.length >= 2) {
            port = Integer.parseInt(args[1]);
        } else {
            port = PORT;
        }

        DatagramSocket sr = new DatagramSocket(port, InetAddress.getLocalHost());

        while (true) {
            byte[] buf = new byte[256];
            DatagramPacket pct = new DatagramPacket(buf, buf.length);
            sr.receive(pct);
            String s = new String(buf);
            System.out.println(s.replaceAll("\0", "") + " " + pct.getAddress().toString());
        }
    } else {
        DatagramSocket ss = new DatagramSocket();
        ss.setBroadcast(true);
        byte[] b = new byte[100];
        DatagramPacket p = new DatagramPacket(b, b.length);
        p.setAddress(InetAddress.getByAddress(new byte[] { (byte) 255,
                (byte) 255, (byte) 255, (byte) 255 }));
        p.setPort(PORT);

        int i = 0;
        while (true) {
            String s = new Integer(i++).toString();
            System.out.println(s);
            b = s.getBytes();
            p.setData(b);
            ss.send(p);
            Thread.sleep(1000);
        }

在机器 A 上,当我同时运行服务器和客户端时,它会立即收到多个数据包,因此当我执行相同操作时,我在其他机器 B 上有以下输出

0 /192.168.253.5
0 /192.168.253.5
1 /192.168.253.5
1 /192.168.253.5
2 /192.168.253.5
2 /192.168.253.5

服务器根本不会收到任何数据包

, 我在机器A上运行客户端,在机器B上运行服务器,

当我在机器A上运行服务器并在机器B上运行客户端时,服务器接收数据包,服务器没有收到任何数据包

我想这取决于发送udp套接字的本地地址,因为每个机器连接到多个网络,并且本地地址是随机选择的(这是真的吗?),并且它只向本地地址所属的网络发送广播,对吗?

如果是这样,我如何向连接的所有网络发送广播? 还为什么收到多个相同的数据包(第一种情况)

I have problem with udp broadcast behaviour,

public static final int PORT = 34567;

public static void main(String[] args) throws IOException,
        InterruptedException {
    if (args.length > 0 && args[0].equals("server")) {
        int port;
        if (args.length >= 2) {
            port = Integer.parseInt(args[1]);
        } else {
            port = PORT;
        }

        DatagramSocket sr = new DatagramSocket(port, InetAddress.getLocalHost());

        while (true) {
            byte[] buf = new byte[256];
            DatagramPacket pct = new DatagramPacket(buf, buf.length);
            sr.receive(pct);
            String s = new String(buf);
            System.out.println(s.replaceAll("\0", "") + " " + pct.getAddress().toString());
        }
    } else {
        DatagramSocket ss = new DatagramSocket();
        ss.setBroadcast(true);
        byte[] b = new byte[100];
        DatagramPacket p = new DatagramPacket(b, b.length);
        p.setAddress(InetAddress.getByAddress(new byte[] { (byte) 255,
                (byte) 255, (byte) 255, (byte) 255 }));
        p.setPort(PORT);

        int i = 0;
        while (true) {
            String s = new Integer(i++).toString();
            System.out.println(s);
            b = s.getBytes();
            p.setData(b);
            ss.send(p);
            Thread.sleep(1000);
        }

on machine A when I run both server and client it receives several packets at once, so I have following output

0 /192.168.253.5
0 /192.168.253.5
1 /192.168.253.5
1 /192.168.253.5
2 /192.168.253.5
2 /192.168.253.5

on other machine B when I do the same, server doesn't receive any packet at all

when I run client on machine A and server on machine B, server receives packets

when I run server on machine A and client on machine B, server doesn't receive any packets

I suppose that it depends on local address of sending udp socket, as every machine is connected to several networks, and local address is chosen randomly (is it true?), and it only sends broadcast to the network which local address belongs to, am I right?

if so, how can I send broadcast to all networks pc is connected?
also why several same packets is received (the first case)

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

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

发布评论

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

评论(2

浊酒尽余欢 2024-11-07 05:58:24

DatagramSocket sr = 新
数据报套接字(端口,
InetAddress.getLocalHost());

将第二个参数更改为 null,或忽略它。您并不关心从哪个 IP 地址接收数据报。

p.setAddress(InetAddress.getByAddress(new
字节[] {(字节)255,
(字节)255,(字节)255,(字节)255 }));

广播至 255.255.255.255 已被弃用约 20 年。使用特定于子网的广播地址。更好的是,研究一下多播。

DatagramSocket sr = new
DatagramSocket(port,
InetAddress.getLocalHost());

Change that 2nd argument to null, or omit it. You don't care which IP address you receive datagrams from.

p.setAddress(InetAddress.getByAddress(new
byte[] { (byte) 255,
(byte) 255, (byte) 255, (byte) 255 }));

Broadcast to 255.255.255.255 has been deprecated for about 20 years. Use a subnet-specific broadcast address. Better still, investigate multicast.

待"谢繁草 2024-11-07 05:58:24

UDP 请求通常仅限于当前子网(除非您的网关通过此后的子网,这是不寻常的)。所以这是一个问题。

未收到任何 UDP 请求的计算机也可能有本地防火墙阻止它。

UDP requests are often restricted to the current subnet (unless you gateway passes then onwards which is unusual). So this is one issue.

The machine not getting any UDP requests could also have a local firewall that is blocking it too.

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