class 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(buf: ByteArray!, length: Int)

Constructs a DatagramPacket for receiving packets of length length.

DatagramPacket(buf: ByteArray!, offset: Int, length: Int)

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

DatagramPacket(buf: ByteArray!, offset: Int, length: Int, address: InetAddress!, port: Int)

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

DatagramPacket(buf: ByteArray!, offset: Int, length: Int, address: SocketAddress!)

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

DatagramPacket(buf: ByteArray!, length: Int, address: InetAddress!, port: Int)

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

DatagramPacket(buf: ByteArray!, length: Int, address: SocketAddress!)

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

Public methods
InetAddress!

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

ByteArray!

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!

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

Unit

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

Unit

Set the data buffer for this packet.

Unit

setData(buf: ByteArray!, offset: Int, length: Int)

Set the data buffer for this packet.

Unit

setLength(length: Int)

Set the length for this packet.

Unit

setPort(iport: Int)

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

Unit

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

Public constructors

DatagramPacket

DatagramPacket(
    buf: ByteArray!,
    length: Int)

Constructs a DatagramPacket for receiving packets of length length.

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

Parameters
buf ByteArray!: buffer for holding the incoming datagram.
length Int: the number of bytes to read.

DatagramPacket

DatagramPacket(
    buf: ByteArray!,
    offset: Int,
    length: Int)

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 ByteArray!: buffer for holding the incoming datagram.
offset Int: the offset for the buffer
length Int: the number of bytes to read.

DatagramPacket

DatagramPacket(
    buf: ByteArray!,
    offset: Int,
    length: Int,
    address: InetAddress!,
    port: Int)

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 ByteArray!: 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.

DatagramPacket

DatagramPacket(
    buf: ByteArray!,
    offset: Int,
    length: Int,
    address: SocketAddress!)

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 ByteArray!: the packet data.
offset Int: the packet data offset.
length Int: the packet data length.
address SocketAddress!: the destination socket address.
Exceptions
java.lang.IllegalArgumentException if address type is not supported

DatagramPacket

DatagramPacket(
    buf: ByteArray!,
    length: Int,
    address: InetAddress!,
    port: Int)

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 ByteArray!: the packet data.
length Int: the packet length.
address InetAddress!: the destination address.
port Int: the destination port number.

DatagramPacket

DatagramPacket(
    buf: ByteArray!,
    length: Int,
    address: SocketAddress!)

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 ByteArray!: the packet data.
length Int: the packet length.
address SocketAddress!: the destination address.
Exceptions
java.lang.IllegalArgumentException if address type is not supported

Public methods

getAddress

fun getAddress(): InetAddress!

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

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

getData

fun getData(): ByteArray!

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.

Return
ByteArray! the buffer used to receive or send data

getLength

fun getLength(): Int

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

Return
Int the length of the data to be sent or the length of the data received.

getOffset

fun getOffset(): Int

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

Return
Int the offset of the data to be sent or the offset of the data received.

getPort

fun getPort(): Int

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

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

getSocketAddress

fun getSocketAddress(): SocketAddress!

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

Return
SocketAddress! the SocketAddress

setAddress

fun setAddress(iaddr: InetAddress!): Unit

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

Parameters
iaddr InetAddress!: the InetAddress

setData

fun setData(buf: ByteArray!): Unit

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 ByteArray!: the buffer to set for this packet.
Exceptions
java.lang.NullPointerException if the argument is null.

See Also

setData

fun setData(
    buf: ByteArray!,
    offset: Int,
    length: Int
): Unit

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

Parameters
buf ByteArray!: 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
Exceptions
java.lang.NullPointerException if the argument is null

setLength

fun setLength(length: Int): Unit

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.
Exceptions
java.lang.IllegalArgumentException if the length is negative of if the length is greater than the packet's data buffer length.

setPort

fun setPort(iport: Int): Unit

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

Parameters
iport Int: the port number

setSocketAddress

fun setSocketAddress(address: SocketAddress!): Unit

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

Parameters
address SocketAddress!: the SocketAddress
Exceptions
java.lang.IllegalArgumentException if address is null or is a SocketAddress subclass not supported by this socket

Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.

Last updated 2026-02-26 UTC.