Get address and port from DatagramPacket
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
class Main {
private final static int BUFSIZE = 20;
public static void main(String args[]) throws Exception {
int port = Integer.parseInt(args[0]);
DatagramSocket ds = new DatagramSocket(port);
byte buffer[] = new byte[BUFSIZE];
while (true) {
DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
ds.receive(dp);
InetAddress ia = dp.getAddress();
System.out.println(ia);
System.out.println(dp.getPort());
}
}
}
Related examples in the same category