michael nesterenko
michael nesterenko

Reputation: 14449

java udp broadcast problem

I have problem with udp broadcast behaviour,

public static final int PORT = 34567;

public static void main(String[] args) throws IOException,
        InterruptedException {
    if (args.length > 0 && args[0].equals("server")) {
        int port;
        if (args.length >= 2) {
            port = Integer.parseInt(args[1]);
        } else {
            port = PORT;
        }

        DatagramSocket sr = new DatagramSocket(port, InetAddress.getLocalHost());

        while (true) {
            byte[] buf = new byte[256];
            DatagramPacket pct = new DatagramPacket(buf, buf.length);
            sr.receive(pct);
            String s = new String(buf);
            System.out.println(s.replaceAll("\0", "") + " " + pct.getAddress().toString());
        }
    } else {
        DatagramSocket ss = new DatagramSocket();
        ss.setBroadcast(true);
        byte[] b = new byte[100];
        DatagramPacket p = new DatagramPacket(b, b.length);
        p.setAddress(InetAddress.getByAddress(new byte[] { (byte) 255,
                (byte) 255, (byte) 255, (byte) 255 }));
        p.setPort(PORT);

        int i = 0;
        while (true) {
            String s = new Integer(i++).toString();
            System.out.println(s);
            b = s.getBytes();
            p.setData(b);
            ss.send(p);
            Thread.sleep(1000);
        }

on machine A when I run both server and client it receives several packets at once, so I have following output

0 /192.168.253.5
0 /192.168.253.5
1 /192.168.253.5
1 /192.168.253.5
2 /192.168.253.5
2 /192.168.253.5

on other machine B when I do the same, server doesn't receive any packet at all

when I run client on machine A and server on machine B, server receives packets

when I run server on machine A and client on machine B, server doesn't receive any packets

I suppose that it depends on local address of sending udp socket, as every machine is connected to several networks, and local address is chosen randomly (is it true?), and it only sends broadcast to the network which local address belongs to, am I right?

if so, how can I send broadcast to all networks pc is connected? also why several same packets is received (the first case)

Upvotes: 1

Views: 4144

Answers (2)

user207421
user207421

Reputation: 311023

DatagramSocket sr = new DatagramSocket(port, InetAddress.getLocalHost());

Change that 2nd argument to null, or omit it. You don't care which IP address you receive datagrams from.

p.setAddress(InetAddress.getByAddress(new byte[] { (byte) 255, (byte) 255, (byte) 255, (byte) 255 }));

Broadcast to 255.255.255.255 has been deprecated for about 20 years. Use a subnet-specific broadcast address. Better still, investigate multicast.

Upvotes: 1

Stephen Gennard
Stephen Gennard

Reputation: 1910

UDP requests are often restricted to the current subnet (unless you gateway passes then onwards which is unusual). So this is one issue.

The machine not getting any UDP requests could also have a local firewall that is blocking it too.

Upvotes: 1

Related Questions