Android APIs
public final class

DatagramPacket

extends Object
java.lang.Object
   ↳ java.net.DatagramPacket

Class Overview

This class represents a datagram packet which contains data either to be sent or received through a DatagramSocket. It holds additional information such as its source or destination host.

See also:

Summary

Public Constructors
DatagramPacket(byte[] data, int length)
Constructs a new DatagramPacket object to receive data up to length bytes.
DatagramPacket(byte[] data, int offset, int length)
Constructs a new DatagramPacket object to receive data up to length bytes with a specified buffer offset.
DatagramPacket(byte[] data, int offset, int length, InetAddress host, int aPort)
Constructs a new DatagramPacket object to send data to the port aPort of the address host.
DatagramPacket(byte[] data, int length, InetAddress host, int port)
Constructs a new DatagramPacket object to send data to the port aPort of the address host.
DatagramPacket(byte[] data, int length, SocketAddress sockAddr)
Constructs a new DatagramPacket object to send data to the address sockAddr.
DatagramPacket(byte[] data, int offset, int length, SocketAddress sockAddr)
Constructs a new DatagramPacket object to send data to the address sockAddr.
Public Methods
InetAddress getAddress()
Gets the sender or destination IP address of this datagram packet.
byte[] getData()
Gets the data of this datagram packet.
int getLength()
Gets the length of the data stored in this datagram packet.
int getOffset()
Gets the offset of the data stored in this datagram packet.
int getPort()
Gets the port number of the target or sender host of this datagram packet.
SocketAddress getSocketAddress()
Gets the host address and the port to which this datagram packet is sent as a SocketAddress object.
void setAddress(InetAddress addr)
Sets the IP address of the target host.
void setData(byte[] buf)
Sets the data buffer for this datagram packet.
void setData(byte[] data, int offset, int byteCount)
Sets the data buffer for this datagram packet.
void setLength(int length)
Sets the length of the datagram packet.
void setPort(int aPort)
Sets the port number of the target host of this datagram packet.
void setSocketAddress(SocketAddress sockAddr)
Sets the SocketAddress for this datagram packet.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public DatagramPacket (byte[] data, int length)

Added in API level 1

Constructs a new DatagramPacket object to receive data up to length bytes.

Parameters
data byte: a byte array to store the read characters.
length int: the length of the data buffer.

public DatagramPacket (byte[] data, int offset, int length)

Added in API level 1

Constructs a new DatagramPacket object to receive data up to length bytes with a specified buffer offset.

Parameters
data byte: a byte array to store the read characters.
offset int: the offset of the byte array where the bytes is written.
length int: the length of the data.

public DatagramPacket (byte[] data, int offset, int length, InetAddress host, int aPort)

Added in API level 1

Constructs a new DatagramPacket object to send data to the port aPort of the address host. The length must be lesser than or equal to the size of data. The first length bytes from the byte array position offset are sent.

Parameters
data byte: a byte array which stores the characters to be sent.
offset int: the offset of data where to read from.
length int: the length of data.
host InetAddress: the address of the target host.
aPort int: the port of the target host.

public DatagramPacket (byte[] data, int length, InetAddress host, int port)

Added in API level 1

Constructs a new DatagramPacket object to send data to the port aPort of the address host. The length must be lesser than or equal to the size of data. The first length bytes are sent.

Parameters
data byte: a byte array which stores the characters to be sent.
length int: the length of data.
host InetAddress: the address of the target host.
port int: the port of the target host.

public DatagramPacket (byte[] data, int length, SocketAddress sockAddr)

Added in API level 1

Constructs a new DatagramPacket object to send data to the address sockAddr. The length must be lesser than or equal to the size of data. The first length bytes of the data are sent.

Parameters
data byte: the byte array to store the data.
length int: the length of the data.
sockAddr SocketAddress: the target host address and port.
Throws
SocketException if an error in the underlying protocol occurs.

public DatagramPacket (byte[] data, int offset, int length, SocketAddress sockAddr)

Added in API level 1

Constructs a new DatagramPacket object to send data to the address sockAddr. The length must be lesser than or equal to the size of data. The first length bytes of the data are sent.

Parameters
data byte: the byte array to store the data.
offset int: the offset of the data.
length int: the length of the data.
sockAddr SocketAddress: the target host address and port.
Throws
SocketException if an error in the underlying protocol occurs.

Public Methods

public InetAddress getAddress ()

Added in API level 1

Gets the sender or destination IP address of this datagram packet.

Returns
InetAddress the address from where the datagram was received or to which it is sent.

public byte[] getData ()

Added in API level 1

Gets the data of this datagram packet.

Returns
byte[] the received data or the data to be sent.

public int getLength ()

Added in API level 1

Gets the length of the data stored in this datagram packet.

Returns
int the length of the received data or the data to be sent.

public int getOffset ()

Added in API level 1

Gets the offset of the data stored in this datagram packet.

Returns
int the position of the received data or the data to be sent.

public int getPort ()

Added in API level 1

Gets the port number of the target or sender host of this datagram packet.

Returns
int the port number of the origin or target host.

public SocketAddress getSocketAddress ()

Added in API level 1

Gets the host address and the port to which this datagram packet is sent as a SocketAddress object.

Returns
SocketAddress the SocketAddress of the target host.

public void setAddress (InetAddress addr)

Added in API level 1

Sets the IP address of the target host.

Parameters
addr InetAddress: the target host address.

public void setData (byte[] buf)

Added in API level 1

Sets the data buffer for this datagram packet. The length of the datagram packet is set to the buffer length.

Parameters
buf byte: the buffer to store the data.

public void setData (byte[] data, int offset, int byteCount)

Added in API level 1

Sets the data buffer for this datagram packet.

Parameters
data byte
offset int
byteCount int

public void setLength (int length)

Added in API level 1

Sets the length of the datagram packet. This length plus the offset must be lesser than or equal to the buffer size.

Parameters
length int: the length of this datagram packet.

public void setPort (int aPort)

Added in API level 1

Sets the port number of the target host of this datagram packet.

Parameters
aPort int: the target host port number.

public void setSocketAddress (SocketAddress sockAddr)

Added in API level 1

Sets the SocketAddress for this datagram packet.

Parameters
sockAddr SocketAddress: the SocketAddress of the target host.