Java DatagramPacket.setData(byte[] buf)
Syntax
DatagramPacket.setData(byte[] buf) has the following syntax.
public void setData(byte[] buf)
Example
In the following code shows how to use DatagramPacket.setData(byte[] buf) method.
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
/*from w w w. j a v a 2 s . c o m*/
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);
dp.setData(buffer);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}