Source Code : Get data from DatagramPacket

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

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

 

Thank with us