我在同一个子网上有一个Android客户端和普通Java服务器,我正在从客户端向服务器发送UDP广播数据包。对于一些路由器(Netgear,Cisco),服务器很乐意接收数据包,但是对于我的Asus路由器,而服务器 machine 接收数据包,服务器 DatagramSocket 不会。
NB在所有情况下,Wireshark显示数据包已到达服务器计算机。但是当使用华硕路由器时,DatagramSocket似乎并没有看到它们。为了简单起见,服务器只与路由器建立了以太网连接。
代码非常标准。
客户端:
DatagramSocket socket = null;
try {
socket = new DatagramSocket();
socket.setSoTimeout(500); // 500 millis
while (isRunning()) {
final InetAddress broadcastAddress = getSubnetBroadcastAddress();
final DatagramPacket outboundPacket = new DatagramPacket(REQUEST_MESSAGE, REQUEST_MESSAGE.length, broadcastAddress, broadcastPort);
socket.send(outboundPacket);
}
} catch (IOException e) {
Log.i(TAG, "Beacon failed", e);
} finally {
if (socket != null) {
socket.close();
}
}
private InetAddress getSubnetBroadcastAddress() throws IOException {
final WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
final DhcpInfo dhcp = wifi.getDhcpInfo();
if (dhcp == null) {
// No successful DHCP request. Go with best effort.
Log.d(TAG, "#getBroadcastAddress - No DHCP info so using generic broadcast address");
return InetAddress.getByName("255.255.255.255");
}
final int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
final byte[] quads = new byte[4];
for (int k = 0; k < 4; k++) {
quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
}
return InetAddress.getByAddress(quads);
}
服务器:
DatagramSocket socket = null;
try {
socket = new DatagramSocket(broadcastPort);
socket.setSoTimeout(LISTENING_TIMEOUT);
socket.setBroadcast(true);
while (keepRunning) {
try {
final byte[] buffer = new byte[1024];
final DatagramPacket receivedPacket = new DatagramPacket(buffer, buffer.length);
socket.receive(receivedPacket);
log.debug("Received packet : " + receivedPacket.toString());
} catch (SocketTimeoutException e) {
log.debug("#run BeaconRunnable socket timed out");
}
}
} catch (IOException e) {
log.warn("Error while receiving message", e);
if (socket != null) {
socket.close();
}
}
导致DatagramSocket无法使用华硕路由器查看UDP广播数据包的原因是什么?
答案 0 :(得分:2)
对于那些跟随...
上面的代码没有任何问题。 路由器配置没有任何问题。
Windows防火墙中添加了一条规则,阻止来自公共连接的传入UDP,并且出于某种原因,Asus路由器被视为公共连接。
禁用该规则可以让一切正常。