我正在尝试在raspberry pi上接收UDP数据包。我创建了一个简单的java程序来连接网络。 (我有以下所有相关代码)
String myNetworkAddress = "10.0.0.11";
String myBroadcastAddress = "10.255.255.255";
// Setup network connection
try {
InetAddress nicAddress = InetAddress.getByName(myNetworkAddress);
InetAddress baddr = InetAddress.getByName(myBroadcastAddress);
dmx = new ArtNet(nicAddress, baddr);
udpsocket = new DatagramSocket(null);
udpsocket.setReuseAddress(true);
udpsocket.bind(new InetSocketAddress(nicAddress, dmx.getPort()));
udpsocket.setBroadcast(true);
udpsocket.setSoTimeout(1000);
System.out.println("Opened socket " + udpsocket.getLocalAddress() + ":" + udpsocket.getLocalPort());
} catch (Exception e) {
System.out.println("can't open socket " + e);
}
while (true) {
if (udpsocket != null) {
if (dmx.readPacket(udpsocket)) {
i = dmx.getPacket();
// read packet and process
}
}
}
我无法从覆盆子pi的广播中收到任何内容。但是当我在另一台计算机上运行此代码时,我能够从广播中接收数据包。这让我相信java没有权限创建套接字,绑定端口,它不是代码,而是覆盆子pi上的安全措施。
我尝试过的事情:
的ifconfig:
eth0 Link encap:Ethernet HWaddr b8:27:eb:11:11:11
inet addr:192.168.1.48 Bcast:192.168.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:351 errors:0 dropped:0 overruns:0 frame:0
TX packets:376 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:25333 (24.7 KiB) TX bytes:77137 (75.3 KiB)
dhclient.conf(当没有路由器/ dhcp服务器可用时,在计算机和带有静态IP的覆盆子pi之间建立连接时使用):
timeout 10;
lease {
interface "eth0";
fixed-address 10.0.0.10;
option broadcast-address 10.255.255.255;
option subnet-mask 255.0.0.0;
option host-name "node.local";
renew 2 2022/1/1 00:00:01;
rebind 2 2022/1/1 00:00:01;
expire 2 2022/1/1 00:00:01;
}
没有运气。任何建议或帮助将不胜感激。
答案 0 :(得分:2)
通过绑定在NicAddress上,它只接受来自网络地址的传入数据包。绑定到0.0.0.0将允许来自广播IP的传入协议以及其他IP地址