Java 中的 UDP 广播
早晨。
我对 Java 和套接字连接很陌生,但我正在尝试在端口 8001 上的 255.255.255.255 上向设备发送 UDP 数据包/广播。我可以很好地发送数据,但是当接收数据时连接超时。我有一个数据包嗅探器,我可以看到数据包发送,然后设备响应。
我很确定这是我在代码中遗漏的菜鸟错误,但我已经坚持了一段时间,任何帮助将不胜感激。
m_Socket = new DatagramSocket(m_SERVERPORT);
InetAddress address = InetAddress.getByName(m_SERVERIP);
m_DataPack = new DatagramPacket(m_SERVERCMD.getBytes(), m_SERVERCMD.getBytes().length,
address, m_SERVERPORT);
m_Socket.setBroadcast(true);
m_Socket.connect(address, m_SERVERPORT);
m_Socket.send(m_DataPack);
m_DataPack = new DatagramPacket(data, data.length,
address, m_SERVERPORT);
m_Socket.receive(m_DataPack); // This is where it times out
data = m_DataPack.getData();
String received = data.toString();
System.out.println("Received: " + received);
m_Socket.close();
谢谢,还有 Gig'Em。
编辑:
我不确定这是否有帮助,但是当我观看 m_Socket 对象时,我可以在发送之前看到以下内容:
bound = true;
close = false;
connectedAddress = Inet4Address (id = 32) (-1,-1,-1,-1);
connectedPort = 8001;
connectState = 1;
created = true;
impl = PlainDatagramSocketImpl;
oldImpl = false;
m_DataPack 对象如下:
address = Inet4Address (id = 32) (-1,-1,-1,-1);
bufLength = 6 (size of packet I'm sending is 6 char long);
offset = 0;
port = 8001;
Morning.
I'm pretty new in Java and socket connections but I'm trying to send out a UDP packet/broadcast on 255.255.255.255 on port 8001 to a device. I can get the data to send just fine, however when it comes time to receive the data the connection times out. I have a packet sniffer and I can see the packet send and then the device respond.
I'm pretty sure it is a rookie mistake that I'm missing in my code but I've been stuck on it for awhile and any help would be appreciated.
m_Socket = new DatagramSocket(m_SERVERPORT);
InetAddress address = InetAddress.getByName(m_SERVERIP);
m_DataPack = new DatagramPacket(m_SERVERCMD.getBytes(), m_SERVERCMD.getBytes().length,
address, m_SERVERPORT);
m_Socket.setBroadcast(true);
m_Socket.connect(address, m_SERVERPORT);
m_Socket.send(m_DataPack);
m_DataPack = new DatagramPacket(data, data.length,
address, m_SERVERPORT);
m_Socket.receive(m_DataPack); // This is where it times out
data = m_DataPack.getData();
String received = data.toString();
System.out.println("Received: " + received);
m_Socket.close();
Thanks and Gig'Em.
EDIT:
I'm not sure if this helps but when I watch the m_Socket object I can see the following right before it sends:
bound = true;
close = false;
connectedAddress = Inet4Address (id = 32) (-1,-1,-1,-1);
connectedPort = 8001;
connectState = 1;
created = true;
impl = PlainDatagramSocketImpl;
oldImpl = false;
and the m_DataPack object is the following:
address = Inet4Address (id = 32) (-1,-1,-1,-1);
bufLength = 6 (size of packet I'm sending is 6 char long);
offset = 0;
port = 8001;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这没有道理。你在广播,这是1对多,你也在连接,这是1对1。是哪一个?
失去连接。并丢失 255.255.255.255。大约 20 年来,该方法已被严重弃用。使用子网本地广播地址,例如 192.168.1.255。
This doesn't make sense. You are broadcasting, which is 1-to-many, and you are also connecting, which is 1-to-1. Which is it?
Lose the connect. And lose the 255.255.255.255. This has been heavily deprecated for about 20 years. Use a subnet-local broadcast address, e.g. 192.168.1.255.
您还可以查看 Broadcasting to Multiple Recipients。希望这有帮助。
You can also take a look at MulticastSocket described at Broadcasting to Multiple Recipients. Hope this helps.
如果您想接收数据报,您需要 bind() 到本地端点(地址+端口)。
If you want to receive a datagram you need to bind() to the local endpoint (address + port).
您正在同一设备上发送和接收数据包。您可以分离发送和接收端口(例如,在 8001 上发送,在 8002 上接收)。并将发送和接收代码作为单独的线程运行。如果两个设备必须找到对方(或一个设备找到自己)。
接收:
发送:
当然应该将这段代码放在线程中的循环中。
基于此示例: https://demey.io/network-discovery-using- udp-broadcast/
更新
上面的链接已失效。但这里也描述了相同的方法: https://www.baeldung.com/java-broadcast-multicast
You are sending and receiving the packet on same device. You could separate send and receive ports (e.g send on 8001, receive on 8002). And run send and receive codes as separate threads. If both device must find each other (or one device find itself).
Receiving:
Sending:
Of course should put this code in a loop in a thread.
Based on this example: https://demey.io/network-discovery-using-udp-broadcast/
UPDATE
The link above is dead. But same method described here also: https://www.baeldung.com/java-broadcast-multicast