Java 上的 UDP 广播不起作用
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我找到了答案。 Windows 7 不再支持 255.255.255.255 广播,显然这为各种威胁提供了机会。要进行广播,需要使用一种不同的方法。
这是维基百科的一个小解释:
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: