Get data from DatagramPacket : DatagramPacket « Network Protocol « Java






Get data from DatagramPacket

import java.net.DatagramPacket;
import java.net.DatagramSocket;

class DatagramReceiver {
  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);
      System.out.println(new String(dp.getData()));
    }
  }
}

 








Related examples in the same category

1.Get address and port from DatagramPacket
2.Create DatagramPacket from byte array
3.Sends the specified text or file as a datagram to the specified port of the specified host
4.Waits to receive datagrams sent the specified portWaits to receive datagrams sent the specified port