Java 上的 UDP 广播不起作用

发布于 2024-12-05 08:35:07 字数 918 浏览 0 评论 0原文

我正在尝试在 IP 地址“255.255.255.255”上发送 UDP 广播,以便在我的网络中发现设备。程序执行,但我在 Wireshark 中看不到任何内容。当我将 IP 地址更改为网络中的已知 IP 时,我可以在 Wireshark 中看到数据包。这是怎么回事 ?

这是我的代码:

public static void main(String args[]) throws Exception
{
    String Broadcastaddress = new String("255.255.255.255");
    int port = 9876;
    DatagramSocket serverSocket = new DatagramSocket();
    serverSocket.setBroadcast(true);
    InetAddress IPAddress = InetAddress.getByName(Broadcastaddress);
    System.out.println("Sending Discovery message to " + IPAddress + "Via UDP port " + port);

    byte[] sendData = new byte[4];
    sendData[0] = 'F';
    sendData[1] = 'I';
    sendData[2] = 'N';
    sendData[3] = 'D';

    DatagramPacket sendPacket = new DatagramPacket(sendData,sendData.length,IPAddress,port);

    while (true)
    {
        serverSocket.send(sendPacket);
        System.out.println("Packet sent");
    }


}

I'm trying to send a UDP broadcast on IP address "255.255.255.255" for device discovery in my network. The program executes, but I don't see anything in Wireshark. when I'm changing the IP address to a known IP in my network, I can see the packets in Wireshark. what's going on ?

This is my code:

public static void main(String args[]) throws Exception
{
    String Broadcastaddress = new String("255.255.255.255");
    int port = 9876;
    DatagramSocket serverSocket = new DatagramSocket();
    serverSocket.setBroadcast(true);
    InetAddress IPAddress = InetAddress.getByName(Broadcastaddress);
    System.out.println("Sending Discovery message to " + IPAddress + "Via UDP port " + port);

    byte[] sendData = new byte[4];
    sendData[0] = 'F';
    sendData[1] = 'I';
    sendData[2] = 'N';
    sendData[3] = 'D';

    DatagramPacket sendPacket = new DatagramPacket(sendData,sendData.length,IPAddress,port);

    while (true)
    {
        serverSocket.send(sendPacket);
        System.out.println("Packet sent");
    }


}

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

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

发布评论

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

评论(1

毁梦 2024-12-12 08:35:07

好的,我找到了答案。 Windows 7 不再支持 255.255.255.255 广播,显然这为各种威胁提供了机会。要进行广播,需要使用一种不同的方法。

这是维基百科的一个小解释:

通过将子网掩码的位补码与主机的IP地址进行按位逻辑或运算,可以获得IPv4主机的广播地址。
示例:使用私有 IP 地址空间 100.16.0.0/12 将数据包广播到整个 IPv4 子网,其子网掩码为 255.240.0.0,广播地址为:100.16.0.0 | 0.15.255.255 = 100.31.255.255。

IP 广播地址 255.255.255.255 存在特殊定义。它是零网络的广播地址或0.0.0.0,在互联网协议标准中代表该网络,即本地网络。根据定义,到此地址的传输受到限制,因为连接本地网络到互联网的路由器永远不会转发该地址。

OK, I found an answer. Windows 7 doesn't support 255.255.255.255 broadcasts anymore, apparently it was an opening to various threats. To broadcast, one needs to use a different approach.

This is a small explenation from Wikipedia:

The broadcast address for an IPv4 host can be obtained by performing a bitwise logical OR operation between the bit complement of the subnet mask and the host's IP address.
Example: to broadcast a packet to an entire IPv4 subnet using the private IP address space 100.16.0.0/12, which has the subnet mask 255.240.0.0, the broadcast address is: 100.16.0.0 | 0.15.255.255 = 100.31.255.255.

A special definition exists for the IP broadcast address 255.255.255.255. It is the broadcast address of the zero network or 0.0.0.0, which in Internet Protocol standards stands for this network, i.e. the local network. Transmission to this address is limited by definition, in that it is never forwarded by the routers connecting the local network to the Internet.

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