Java DatagramPacket.setLength(int length)

Syntax

DatagramPacket.setLength(int length) has the following syntax.

public void setLength(int length)

Example

In the following code shows how to use DatagramPacket.setLength(int length) method.


import java.net.DatagramPacket;
import java.net.DatagramSocket;
//from ww w.jav  a  2  s  . c  o m
public class Main {
  private final static int BUFSIZE = 20;

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

      int port = 80;

      DatagramSocket ds = new DatagramSocket(port);

      byte buffer[] = new byte[BUFSIZE];

      while (true) {

        DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
        // Receive data
        ds.receive(dp);
        
        dp.setLength(10);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}




















Home »
  Java Tutorial »
    java.net »




CookieManager
CookiePolicy
CookieStore
DatagramPacket
DatagramSocket
HttpCookie
HttpURLConnection
InetAddress
JarURLConnection
MulticastSocket
ServerSocket
Socket
SocketAddress
URI
URL
URLConnection
URLDecoder
URLEncoder