1
votes

I am attempting to broadcast a UDP packet over all NetworkInterfaces and receive the replies. While I am able to receive responses from the local network, a device connected via a crossover connection is not able receive a reply.

This is the code to get all Interfaces, which does return the crossover connection's NetworkInterface

    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while(interfaces.hasMoreElements()){
            List<InterfaceAddress> iAddrs = interfaces.nextElement().getInterfaceAddresses();
            iAddrs.forEach(addr -> {
                if(addr.getBroadcast() != null){
                    System.out.println(addr.getBroadcast());

                    sendUDP(addr.getBroadcast());
                }
            });
        }

I then use this section of code to send the UDP packet and listen for responses.

    final DatagramSocket socket = new DatagramSocket(9800);
    socket.setBroadcast(true);
    socket.setSoTimeout(5000);
    final byte[] data = "A-UDP-BROADCAST".getBytes();
    byte[] buffer = new byte[1024];

    socket.send(new DatagramPacket(data, data.length, addr, 9800));

    while (true) {
        try {
            final DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
            socket.receive(packet);
            System.out.println(new String(packet.getData()));

        } catch (SocketTimeoutException e) {
            System.out.println("Timed out.");
            return;
        }
        buffer = new byte[1024];
    }

This must be an issue in this section of code, as when I send a packet with an external program such as PacketSender I am able to receive a reply.

1
If it works on localhost it is clearly not an issue with the code. It is a topology or routing or cable problem.user207421
@EJP PacketSender, which is a third party TCP/UDP debugging application, is able to receive the responses, so there must be a fault with the code. Its either that or a JVM issue and I'm not going to say that without a lot more testing.Beryllium
"Connected via a crossover connection". Does this mean a cross-over cable? That's a physical layer issue, and in modern equipment almost always handled automatically and transparently by the network interfaces. If you are able to communicate at all between the two systems, it's difficult to see how that can be any part of this problem. I would use wireshark to determine what packets are actually being sent between the two systems.Gil Hamilton
@GilHamilton Yes, It is connected via a crossover cable. Wireshark is reporting that a packet is being broadcast and that a response is being given back. It would seem like 'socket.receive()' isn't receiving that reply.Beryllium
Not sure. A firewall issue perhaps?Gil Hamilton

1 Answers

1
votes

What seems wrong to me about your code is how you bind both receiving and sending socket to the same port. Without setReuseAddress() one of the binds should fail. To fix that, simply change the first line in the second piece of code to use any free port:

final DatagramSocket socket = new DatagramSocket();