new DatagramPacket(byte[] buffer, int bufferLength) : DatagramPacket « java.net « Java by API






new DatagramPacket(byte[] buffer, int bufferLength)

/*
 * Output:
 *  
 */

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

public class MainClass {
  private final static int BUFSIZE = 20;

  public static void main(String args[]) {
    try {

      int port = 80;

      DatagramSocket ds = new DatagramSocket(port);

      while (true) {
        byte buffer[] = new byte[BUFSIZE];

        DatagramPacket dp = new DatagramPacket(buffer, buffer.length);

        ds.receive(dp);

        String str = new String(dp.getData());

        System.out.println(str);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

           
       








Related examples in the same category

1.new DatagramPacket(byte[] buf, int length, InetAddress address, int port)
2.DatagramPacket: getAddress()
3.DatagramPacket: getData()
4.DatagramPacket: getPort()
5.DatagramPacket: getSocketAddress()