简体   繁体   中英

Java DatagramSocket not receiving UDP broadcast from some routers

I have an Android client and plain Java server both on the same subnet and I am sending UDP broadcast packets from client to server. With some routers (Netgear, Cisco) the server happily receives the packets, but for my Asus router while the server machine receives the packets the server DatagramSocket doesn't.

NB In all cases, Wireshark shows that the packets are getting to the server machine. But when using the Asus router the DatagramSocket doesn't seem to see them. To keep things simple the server only has an ethernet connection to the router.

The code is pretty standard.

Client:

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);
}

Server:

    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();
        }
    }

What's causing the DatagramSocket to not see the UDP broadcast packets with the Asus router?

For those that follow ..

There is nothing wrong with the code above. There was nothing wrong with the router config.

A rule had been added to Windows Firewall that blocked incoming UDP from public connections and the Asus router for some reason was deemed to be a public connection.

Disabling that rule let everything work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM