public final class DatagramPacket extends Object
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.
Unless otherwise specified, passing a null
argument causes a NullPointerException
to be thrown.
Methods and constructors of DatagramPacket
accept parameters of type SocketAddress
. DatagramPacket
supports InetSocketAddress
, and may support additional SocketAddress
sub-types.
Constructor | Description |
---|---|
DatagramPacket |
Constructs a DatagramPacket for receiving packets of length length . |
DatagramPacket |
Constructs a DatagramPacket for receiving packets of length length , specifying an offset into the buffer. |
DatagramPacket |
Constructs a datagram packet for sending packets of length length with offset offset to the specified port number on the specified host. |
DatagramPacket |
Constructs a datagram packet for sending packets of length length with offset offset to the specified port number on the specified host. |
DatagramPacket |
Constructs a datagram packet for sending packets of length length to the specified port number on the specified host. |
DatagramPacket |
Constructs a datagram packet for sending packets of length length to the specified port number on the specified host. |
Modifier and Type | Method | Description |
---|---|---|
InetAddress |
getAddress() |
Returns the IP address of the machine to which this datagram is being sent or from which the datagram was received, or null if not set. |
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, or 0 if not set. |
SocketAddress |
getSocketAddress() |
Returns the SocketAddress (usually IP address + port number) of the remote host that this packet is being sent to or is coming from. |
void |
setAddress |
Sets the IP address of the machine to which this datagram is being sent. |
void |
setData |
Set the data buffer for this packet. |
void |
setData |
Set the data buffer for this packet. |
void |
setLength |
Set the length for this packet. |
void |
setPort |
Sets the port number on the remote host to which this datagram is being sent. |
void |
setSocketAddress |
Sets the SocketAddress (usually IP address + port number) of the remote host to which this datagram is being sent. |
public DatagramPacket(byte[] buf, int offset, int length)
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
.
buf
- buffer for holding the incoming datagram.offset
- the offset for the bufferlength
- the number of bytes to read.IllegalArgumentException
- if the length or offset is negative, or if the length plus the offset is greater than the length of the packet's given buffer.public DatagramPacket(byte[] buf, int length)
DatagramPacket
for receiving packets of length length
. The length
argument must be less than or equal to buf.length
.
buf
- buffer for holding the incoming datagram.length
- the number of bytes to read.IllegalArgumentException
- if the length is negative or if the length is greater than the length of the packet's given buffer.public DatagramPacket(byte[] buf, int offset, int length, InetAddress address, int port)
length
with offset offset
to the specified port number on the specified host. The length
argument must be less than or equal to buf.length
.buf
- the packet data.offset
- the packet data offset.length
- the packet data length.address
- the destination address, or null
.port
- the destination port number.IllegalArgumentException
- if the length or offset is negative, or if the length plus the offset is greater than the length of the packet's given buffer, or if the port is out of range.public DatagramPacket(byte[] buf, int offset, int length, SocketAddress address)
length
with offset offset
to the specified port number on the specified host. The length
argument must be less than or equal to buf.length
.buf
- the packet data.offset
- the packet data offset.length
- the packet data length.address
- the destination socket address.IllegalArgumentException
- if address is null or its type is not supported, or if the length or offset is negative, or if the length plus the offset is greater than the length of the packet's given buffer.public DatagramPacket(byte[] buf, int length, InetAddress address, int port)
length
to the specified port number on the specified host. The length
argument must be less than or equal to buf.length
.buf
- the packet data.length
- the packet length.address
- the destination address, or null
.port
- the destination port number.IllegalArgumentException
- if the length is negative, or if the length is greater than the length of the packet's given buffer, or if the port is out of range.public DatagramPacket(byte[] buf, int length, SocketAddress address)
length
to the specified port number on the specified host. The length
argument must be less than or equal to buf.length
.buf
- the packet data.length
- the packet length.address
- the destination address.IllegalArgumentException
- if address is null or its type is not supported, or if the length is negative, or if the length is greater than the length of the packet's given buffer, or if the port is out of range.public InetAddress getAddress()
null
if not set.public int getPort()
public byte[] getData()
offset
in the buffer, and runs for length
long.public int getOffset()
public int getLength()
public void setData(byte[] buf, int offset, int length)
buf
- the buffer to set for this packetoffset
- the offset into the datalength
- the length of the data and/or the length of the buffer used to receive dataIllegalArgumentException
- if the length or offset is negative, or if the length plus the offset is greater than the length of the packet's given buffer.public void setAddress(InetAddress iaddr)
iaddr
- the InetAddress
, or null
.public void setPort(int iport)
iport
- the port numberIllegalArgumentException
- if the port is out of rangepublic void setSocketAddress(SocketAddress address)
address
- the SocketAddress
IllegalArgumentException
- if address is null or is a SocketAddress subclass not supported.public SocketAddress getSocketAddress()
SocketAddress
(usually IP address + port number) of the remote host that this packet is being sent to or is coming from.SocketAddress
public void setData(byte[] buf)
buf
.buf
- the buffer to set for this packet.public void setLength(int length)
length
plus the offset
must be lesser or equal to the length of the packet's data buffer.length
- the length to set for this packet.IllegalArgumentException
- if the length is negative, or if the length plus the offset is greater than the length of the packet's data buffer.
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/net/DatagramPacket.html