我已经阅读了关于广播的问答,我有了大致的想法。然而,在使用我的Java代码时,我感到困惑。
我的IP地址是192.168.8.102。当我广播到192.168.8.255时,我可以收到来自自己的数据包,并将其捕获到wireshark上。但是当我广播到255.255.255.255时,wireshark似乎遗漏了它,尽管我的代码仍然可以接收它。当我运行别人的应用程序时,wireshark捕获它广播到255.255.255.255的包。
有什么解释吗?非常感谢!
PS。下面是我的Java代码的一部分:
DatagramSocket senderSocket = new DatagramSocket(null);
senderSocket.setReuseAddress(true);
senderSocket.setBroadcast(true);
senderSocket.bind(new InetSocketAddress(2000));
InetAddress address = InetAddress.getByName("255.255.255.255");
byte[] SendBuffer = contentSent.getBytes();
senderPacket = new DatagramPacket(SendBuffer, SendBuffer.length, address, 2000);
senderSocket.send(senderPacket);发布于 2015-09-07 21:21:42
如果您的套接字未绑定到特定接口,则发送到255.255.255.255的数据包将在默认接口上发出。如果这不是Wireshark正在监听的,这就解释了为什么它看不到它,而你的代码却看到了。
如果您的套接字绑定到特定的接口,这就保证了在该套接字上的发送将在该接口上发出。
正如EJP在评论中提到的,不推荐广播到255.255.255.255,部分原因是我提到的限制。您最好使用有问题的链接的广播地址,例如,在本例中为192.168.8.255。
https://stackoverflow.com/questions/32434052
复制相似问题