Java.net.DatagramPacket.getOffset() Method



Description

The java.net.DatagramPacket.getOffset() returns the offset of the data to be sent or the offset of the data received.

Declaration

Following is the declaration for java.net.DatagramPacket.getOffset() method

public int getOffset()

Parameters

  • NA

Return Value

This method returns the offset of the data to be sent or the offset of the data received.

Exception

  • NA

Example

The following example shows the usage of net.DatagramPacket.getOffset() method

package com.tutorialspoint;

import java.net.*;

class DatagramPacketDemo {

   public static void main(String args[]) throws Exception {

      // create a DatagramSocket object binded to port 9999
      DatagramSocket ds = new DatagramSocket(9999);

      // create a byte array buf
      byte buf[] = new byte[100];

      // create a DatagramPacket object
      DatagramPacket dp = new DatagramPacket(buf, buf.length);

      // recieve the datagram packet into dp
      ds.receive(dp);

      // offset of data received is stored in o
      int o = dp.getOffset();

      // print o value
      System.out.println("Offset of data received is : " + o );
   }
}

Let us compile and run the above program, this will produce the following result:

Offset of data received is : 0
Advertisements