Ignacio Ferrero
Ignacio Ferrero

Reputation: 137

UDp Broadcast packets not received by the server

I am creating a basic android application that sends a broadcast message. In my laptop I have a server listening to all broadcast messages. But the package is never recieved and i don't know why(If I sent it to the specific Server ip, the servers gets the packet) . Here is my code for both, the application and the server.

Server:

        byte[] buffer = new byte[2048];
        int port = 8063;
        DatagramSocket dsocket = new DatagramSocket(port);
        dsocket.setBroadcast(true);
        
        DatagramPacket packet = new DatagramPacket(buffer,
                            buffer.length);
                        
                        
       while (true) 
        {
             System.out.println("Receiving...");
             dsocket.receive(packet);
             System.out.println("received...");
             String msg = new String(buffer, 0, packet.getLength());
             System.out.println(packet.getAddress().getHostName()
                                    + ": " + msg);
             packet.setLength(buffer.length);
         }

Application:

socket = new DatagramSocket();
socket.setBroadcast(true);

DatagramPacket packet = new DatagramPacket(new byte[]{0,1,2,3},4,
            InetAddress.getByName("172.16.255.255"), 8063);
socket.send(packet); 

Any advice on why the server is not receiving the package?

Edit:

It seems the University network is blocking broadcast packages, we tried it with 4g and it worked fine.

Thank you in advance

Upvotes: 0

Views: 1105

Answers (1)

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84239

Broadcast can only reach nodes within the same broadcast domain, while unicast, as in your second example, is IP-routed, so unless your client and server machines are wired into the same switch/hub, or explicitly put into the same VLAN, broadcast will not work for you.

Upvotes: 1

Related Questions