Skip to content

Most visited

Recently visited

navigation

DatagramPacket

public final class DatagramPacket
extends Object

java.lang.Object
   ↳ java.net.DatagramPacket


This class represents a datagram packet.

Datagram packets are used to implement a connectionless packet delivery service. Each message is routed from one machine to another based solely on information contained within that packet. Multiple packets sent from one machine to another might be routed differently, and might arrive in any order. Packet delivery is not guaranteed.

Summary

Public constructors

DatagramPacket(byte[] buf, int offset, int length)

Constructs a DatagramPacket for receiving packets of length length, specifying an offset into the buffer.

DatagramPacket(byte[] buf, int length)

Constructs a DatagramPacket for receiving packets of length length.

DatagramPacket(byte[] buf, int offset, int length, InetAddress address, int port)

Constructs a datagram packet for sending packets of length length with offset ioffsetto the specified port number on the specified host.

DatagramPacket(byte[] buf, int offset, int length, SocketAddress address)

Constructs a datagram packet for sending packets of length length with offset ioffsetto the specified port number on the specified host.

DatagramPacket(byte[] buf, int length, InetAddress address, int port)

Constructs a datagram packet for sending packets of length length to the specified port number on the specified host.

DatagramPacket(byte[] buf, int length, SocketAddress address)

Constructs a datagram packet for sending packets of length length to the specified port number on the specified host.

Public methods

InetAddress getAddress()

Returns the IP address of the machine to which this datagram is being sent or from which the datagram was received.

byte[] getData()

Returns the data buffer.

int getLength()

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

int getOffset()

Returns the offset of the data to be sent or the offset of the data received.

int getPort()

Returns the port number on the remote host to which this datagram is being sent or from which the datagram was received.

SocketAddress getSocketAddress()

Gets the SocketAddress (usually IP address + port number) of the remote host that this packet is being sent to or is coming from.

void setAddress(InetAddress iaddr)

Sets the IP address of the machine to which this datagram is being sent.

void setData(byte[] buf, int offset, int length)

Set the data buffer for this packet.

void setData(byte[] buf)

Set the data buffer for this packet.

void setLength(int length)

Set the length for this packet.

void setPort(int iport)

Sets the port number on the remote host to which this datagram is being sent.

void setSocketAddress(SocketAddress address)

Sets the SocketAddress (usually IP address + port number) of the remote host to which this datagram is being sent.

Inherited methods

From class java.lang.Object

Public constructors

DatagramPacket

added in API level 1
DatagramPacket (byte[] buf, 
                int offset, 
                int length)

Constructs a DatagramPacket for receiving packets of length length, specifying an offset into the buffer.

The length argument must be less than or equal to buf.length.

Parameters
buf byte: buffer for holding the incoming datagram.

offset int: the offset for the buffer

length int: the number of bytes to read.

DatagramPacket

added in API level 1
DatagramPacket (byte[] buf, 
                int length)

Constructs a DatagramPacket for receiving packets of length length.

The length argument must be less than or equal to buf.length.

Parameters
buf byte: buffer for holding the incoming datagram.

length int: the number of bytes to read.

DatagramPacket

added in API level 1
DatagramPacket (byte[] buf, 
                int offset, 
                int length, 
                InetAddress address, 
                int port)

Constructs a datagram packet for sending packets of length length with offset ioffsetto the specified port number on the specified host. The length argument must be less than or equal to buf.length.

Parameters
buf byte: the packet data.

offset int: the packet data offset.

length int: the packet data length.

address InetAddress: the destination address.

port int: the destination port number.

See also:

DatagramPacket

added in API level 1
DatagramPacket (byte[] buf, 
                int offset, 
                int length, 
                SocketAddress address)

Constructs a datagram packet for sending packets of length length with offset ioffsetto the specified port number on the specified host. The length argument must be less than or equal to buf.length.

Android note: Up to and including API 25 this method declared that a SocketException can be thrown, although the exception is never thrown. Code compiled against a newer SDK does not need to catch the exception and will be binary compatible with older versions of Android.

Parameters
buf byte: the packet data.

offset int: the packet data offset.

length int: the packet data length.

address SocketAddress: the destination socket address.

Throws
IllegalArgumentException if address type is not supported

See also:

DatagramPacket

added in API level 1
DatagramPacket (byte[] buf, 
                int length, 
                InetAddress address, 
                int port)

Constructs a datagram packet for sending packets of length length to the specified port number on the specified host. The length argument must be less than or equal to buf.length.

Android note: Up to and including API 25 this method declared that a SocketException can be thrown, although the exception is never thrown. Code compiled against a newer SDK does not need to catch the exception and will be binary compatible with older versions of Android.

Parameters
buf byte: the packet data.

length int: the packet length.

address InetAddress: the destination address.

port int: the destination port number.

See also:

DatagramPacket

added in API level 1
DatagramPacket (byte[] buf, 
                int length, 
                SocketAddress address)

Constructs a datagram packet for sending packets of length length to the specified port number on the specified host. The length argument must be less than or equal to buf.length.

Parameters
buf byte: the packet data.

length int: the packet length.

address SocketAddress: the destination address.

Throws
IllegalArgumentException if address type is not supported

See also:

Public methods

getAddress

added in API level 1
InetAddress getAddress ()

Returns the IP address of the machine to which this datagram is being sent or from which the datagram was received.

Returns
InetAddress the IP address of the machine to which this datagram is being sent or from which the datagram was received.

getData

added in API level 1
byte[] getData ()

Returns the data buffer. The data received or the data to be sent starts from the offset in the buffer, and runs for length long.

Returns
byte[] the buffer used to receive or send data

getLength

added in API level 1
int getLength ()

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

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

See also:

getOffset

added in API level 1
int getOffset ()

Returns the offset of the data to be sent or the offset of the data received.

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

getPort

added in API level 1
int getPort ()

Returns the port number on the remote host to which this datagram is being sent or from which the datagram was received.

Returns
int the port number on the remote host to which this datagram is being sent or from which the datagram was received.

See also:

getSocketAddress

added in API level 1
SocketAddress getSocketAddress ()

Gets the SocketAddress (usually IP address + port number) of the remote host that this packet is being sent to or is coming from.

Returns
SocketAddress the SocketAddress

setAddress

added in API level 1
void setAddress (InetAddress iaddr)

Sets the IP address of the machine to which this datagram is being sent.

Parameters
iaddr InetAddress: the InetAddress

See also:

setData

added in API level 1
void setData (byte[] buf, 
                int offset, 
                int length)

Set the data buffer for this packet. This sets the data, length and offset of the packet.

Parameters
buf byte: the buffer to set for this packet

offset int: the offset into the data

length int: the length of the data and/or the length of the buffer used to receive data

Throws
NullPointerException if the argument is null

setData

added in API level 1
void setData (byte[] buf)

Set the data buffer for this packet. With the offset of this DatagramPacket set to 0, and the length set to the length of buf.

Parameters
buf byte: the buffer to set for this packet.

Throws
NullPointerException if the argument is null.

setLength

added in API level 1
void setLength (int length)

Set the length for this packet. The length of the packet is the number of bytes from the packet's data buffer that will be sent, or the number of bytes of the packet's data buffer that will be used for receiving data. The length must be lesser or equal to the offset plus the length of the packet's buffer.

Parameters
length int: the length to set for this packet.

Throws
IllegalArgumentException if the length is negative of if the length is greater than the packet's data buffer length.

setPort

added in API level 1
void setPort (int iport)

Sets the port number on the remote host to which this datagram is being sent.

Parameters
iport int: the port number

See also:

setSocketAddress

added in API level 1
void setSocketAddress (SocketAddress address)

Sets the SocketAddress (usually IP address + port number) of the remote host to which this datagram is being sent.

Parameters
address SocketAddress: the SocketAddress

Throws
IllegalArgumentException if address is null or is a SocketAddress subclass not supported by this socket

See also:

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)