Receive a Datagram in Java
Description
The following code shows how to receive a Datagram.
Example
/* w w w . j ava2 s . co m*/
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class Main {
public static void main(String[] argv) throws Exception {
byte[] inbuf = new byte[256]; // default size
DatagramSocket socket = new DatagramSocket();
DatagramPacket packet = new DatagramPacket(inbuf, inbuf.length);
socket.receive(packet);
int numBytesReceived = packet.getLength();
System.out.println(numBytesReceived);
}
}