Java DatagramPacket.getLength()
Syntax
DatagramPacket.getLength() has the following syntax.
public int getLength()
Example
In the following code shows how to use DatagramPacket.getLength() method.
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/* w ww . j a v a2 s.c om*/
public class Main {
private final static int BUFSIZE = 20;
public static void main(String args[]) {
try {
int port = 80;
DatagramSocket ds = new DatagramSocket(port);
byte buffer[] = new byte[BUFSIZE];
while (true) {
DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
// Receive data
ds.receive(dp);
// Display address from the datagram packet
InetAddress ia = dp.getAddress();
System.out.println(ia);
// Display port from the datagram packet
System.out.println(dp.getPort());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}