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.
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.
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.
DatagramSocket , which uses the UDP protocol.
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.
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"));
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With