Receiving a Datagram : UDP Client « Network « Java Tutorial






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);
  }
}








19.17.UDP Client
19.17.1.UDP Echo Client
19.17.2.UDP Discard Client
19.17.3.UDP Time Client
19.17.4.Send a Datagram
19.17.5.Sending a Datagram
19.17.6.Receiving a Datagram
19.17.7.Read and write with DatagramPacket
19.17.8.User Datagram Protocol Programming