Top Description Fields Constructors Methods
java.net

public final Class DatagramPacket

extends Object
Class Inheritance
Imports
java.util.Objects, jdk.internal.util.Preconditions

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.

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.

Authors
Pavani Diwanji, Benjamin Renaud
Since
1.0

Field Summary

Modifier and TypeField and Description
pack-priv InetAddress
pack-priv byte[]
pack-priv int
pack-priv int
pack-priv int
pack-priv int

Constructor Summary

AccessConstructor and Description
public
DatagramPacket(byte[]
buffer for holding the incoming datagram.
buf
,
int
the offset for the buffer
offset
,
int
the number of bytes to read.
length
)

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

public
DatagramPacket(byte[]
buffer for holding the incoming datagram.
buf
,
int
the number of bytes to read.
length
)

Constructs a DatagramPacket for receiving packets of length length.

public
DatagramPacket(byte[]
the packet data.
buf
,
int
the packet data offset.
offset
,
int
the packet data length.
length
,
InetAddress
the destination address, or null.
address
,
int
the destination port number.
port
)

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

public
DatagramPacket(byte[]
the packet data.
buf
,
int
the packet data offset.
offset
,
int
the packet data length.
length
,
SocketAddress
the destination socket address.
address
)

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

public
DatagramPacket(byte[]
the packet data.
buf
,
int
the packet length.
length
,
InetAddress
the destination address, or null.
address
,
int
the destination port number.
port
)

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

public
DatagramPacket(byte[]
the packet data.
buf
,
int
the packet length.
length
,
SocketAddress
the destination address.
address
)

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

Method Summary

Modifier and TypeMethod and Description
public synchronized InetAddress

Returns:

the IP address of the machine to which this datagram is being sent or from which the datagram was received.
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.

public synchronized byte[]

Returns:

the buffer used to receive or send data
getData
()

Returns the data buffer.

public synchronized int

Returns:

the length of the data to be sent or the length of the data received.
getLength
()

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

public synchronized int

Returns:

the offset of the data to be sent or the offset of the data received.
getOffset
()

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

public synchronized int

Returns:

the port number on the remote host to which this datagram is being sent or from which the datagram was received.
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.

public synchronized SocketAddress

Returns:

the 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.

public synchronized void
setAddress(InetAddress
the InetAddress, or null.
iaddr
)

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

public synchronized void
setData(byte[]
the buffer to set for this packet
buf
,
int
the offset into the data
offset
,
int
the length of the data and/or the length of the buffer used to receive data
length
)

Set the data buffer for this packet.

public synchronized void
setData(byte[]
the buffer to set for this packet.
buf
)

Set the data buffer for this packet.

public synchronized void
setLength(int
the length to set for this packet.
length
)

Set the length for this packet.

public synchronized void
setPort(int
the port number
iport
)

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

public synchronized void
setSocketAddress(SocketAddress
the SocketAddress
address
)

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

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

addressback to summary
pack-priv InetAddress address
bufback to summary
pack-priv byte[] buf
bufLengthback to summary
pack-priv int bufLength
lengthback to summary
pack-priv int length
offsetback to summary
pack-priv int offset
portback to summary
pack-priv int port

Constructor Detail

DatagramPacketback to summary
public 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.

Exceptions
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.
Since
1.2
DatagramPacketback to summary
public 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.

Exceptions
IllegalArgumentException:
if the length is negative or if the length is greater than the length of the packet's given buffer.
DatagramPacketback to summary
public DatagramPacket(byte[] buf, int offset, int length, InetAddress address, int port)

Constructs a datagram packet for sending packets of length 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.

Parameters
buf:byte[]

the packet data.

offset:int

the packet data offset.

length:int

the packet data length.

address:InetAddress

the destination address, or null.

port:int

the destination port number.

Exceptions
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.
Since
1.2
See Also
java.net.InetAddress
DatagramPacketback to summary
public DatagramPacket(byte[] buf, int offset, int length, SocketAddress address)

Constructs a datagram packet for sending packets of length 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.

Parameters
buf:byte[]

the packet data.

offset:int

the packet data offset.

length:int

the packet data length.

address:SocketAddress

the destination socket address.

Exceptions
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.
Since
1.4
See Also
java.net.InetAddress
DatagramPacketback to summary
public 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.

Parameters
buf:byte[]

the packet data.

length:int

the packet length.

address:InetAddress

the destination address, or null.

port:int

the destination port number.

Exceptions
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.
See Also
java.net.InetAddress
DatagramPacketback to summary
public 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.

Exceptions
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.
Since
1.4
See Also
java.net.InetAddress

Method Detail

getAddressback to summary
public synchronized 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.

Returns:InetAddress

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

See Also
java.net.InetAddress, setAddress(java.net.InetAddress)
getDataback to summary
public synchronized 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

See Also
setData(byte[], int, int)
getLengthback to summary
public synchronized 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
setLength(int)
getOffsetback to summary
public synchronized 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.

Since
1.2
getPortback to summary
public synchronized 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.

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
setPort(int)
getSocketAddressback to summary
public synchronized 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.

Returns:SocketAddress

the SocketAddress

Since
1.4
See Also
setSocketAddress
setAddressback to summary
public synchronized void setAddress(InetAddress iaddr)

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

Parameters
iaddr:InetAddress

the InetAddress, or null.

Since
1.1
See Also
getAddress()
setDataback to summary
public synchronized 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

Exceptions
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.
Since
1.2
See Also
getData, getOffset, getLength
setDataback to summary
public synchronized 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.

Since
1.1
See Also
getLength, getData
setLengthback to summary
public synchronized 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 plus the offset must be lesser or equal to the length of the packet's data buffer.

Parameters
length:int

the length to set for this packet.

Exceptions
IllegalArgumentException:
if the length is negative, or if the length plus the offset is greater than the length of the packet's data buffer.
Since
1.1
See Also
getLength, setData
setPortback to summary
public synchronized 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

Exceptions
IllegalArgumentException:
if the port is out of range
Since
1.1
See Also
getPort()
setSocketAddressback to summary
public synchronized 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

Exceptions
IllegalArgumentException:
if address is null or is a SocketAddress subclass not supported.
Since
1.4
See Also
getSocketAddress