Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java DatagramSocket listening on a broadcast address

Tags:

java

broadcast

I have written the simple test class which is meant to listen on Eth and receive all UDP packets, which go to port 5001:

public class Main {

  public static void main(String[] args) throws SocketException, UnknownHostException, IOException {
    DatagramSocket socket = new DatagramSocket(5001, InetAddress.getByName("255.255.255.255"));
    socket.setBroadcast(true);
    System.out.println("Listen on " + socket.getLocalAddress() + " from " + socket.getInetAddress() + " port " + socket.getBroadcast());
    byte[] buf = new byte[512];
    DatagramPacket packet = new DatagramPacket(buf, buf.length);
    while (true) {
      System.out.println("Waiting for data");
      socket.receive(packet);
      System.out.println("Data received");
    }
  }
}

It does not work anymore. It prints out Waiting for data and never continue. tcpdump shows me, that UDP broadcast packets come. What am I doing wrong? Thank you much.

like image 998
rcs-34 Avatar asked Mar 29 '11 12:03

rcs-34


People also ask

Is DatagramSocket UDP or TCP?

DatagramSockets are Java's mechanism for network communication via UDP instead of TCP. Java provides DatagramSocket to communicate over UDP instead of TCP. It is also built on top of IP. DatagramSockets can be used to both send and receive packets over the Internet.

What is DatagramSocket explain in detail with example?

A datagram socket is the sending or receiving point for a packet delivery service. Each packet sent or received on a datagram socket is individually addressed and routed. Multiple packets sent from one machine to another may be routed differently, and may arrive in any order.

What protocol is used by the DatagramSocket class?

DatagramSocket , which uses the UDP protocol.

Which exception is thrown by DatagramSocket constructor?

Commonly used Constructors of DatagramSocket class DatagramSocket() throws SocketEeption: it creates a datagram socket and binds it with the available Port Number on the localhost machine. DatagramSocket(int port) throws SocketEeption: it creates a datagram socket and binds it with the given Port Number.


2 Answers

Receiver can't listen on a broadcast address.

Broadcast address is for senders - sender can send a packet with 255.255.255.255:5001 as a destination, and all receivers listening that port in a subnet would receive it. But there is no way to create a receiver that can receive "all UDP packets".

If you already have a broadcast sender and want to receive its packets, you need to listen on a wildcard address instead:

DatagramSocket socket = new DatagramSocket(5001, InetAddress.getByName("0.0.0.0")); 
like image 161
axtavt Avatar answered Sep 30 '22 09:09

axtavt


The DatagramSocket socket that you send on must be bound to a port (5001) using

socket = new DatagramSocket(5001);

Then when you send on this socket, use

InetAddress sendAddress = InetAddress.getByName("127.0.0.1");
DatagramPacket packet = new DatagramPacket(buf, buf.length, sendAddress, socket.getLocalPort());
socket.send(packet);  

Then when you listen on another socket, use

DatagramSocket socket = new DatagramSocket(5001, InetAddress.getByName("127.0.0.1"));

and you can listen to UDP data that you sent to port 5001, on port 5001. Not sure why, it's something to do with InetAddress router table using different IP address subnets for the same 127.0.0.1 address?

like image 34
Rup Avatar answered Sep 30 '22 08:09

Rup