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.
Constructor and Description |
---|
DatagramPacket(byte[] buf,
int length)
Constructs a
DatagramPacket for receiving packets of
length length . |
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 offset,
int length)
Constructs a
DatagramPacket for receiving packets of
length length , specifying an offset into the buffer. |
DatagramPacket(byte[] buf,
int offset,
int length,
InetAddress address,
int port)
Constructs a datagram packet for sending packets of length
length with offset ioffset to 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 ioffset 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. |
Modifier and Type | Method and Description |
---|---|
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)
Set the data buffer for this packet.
|
void |
setData(byte[] buf,
int offset,
int length)
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.
|
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.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.public DatagramPacket(byte[] buf, int offset, int length, InetAddress address, int port)
length
with offset ioffset
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.port
- the destination port number.InetAddress
public DatagramPacket(byte[] buf, int offset, int length, SocketAddress address)
length
with offset ioffset
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 type is not supportedInetAddress
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.port
- the destination port number.InetAddress
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 type is not supportedInetAddress
public InetAddress getAddress()
InetAddress
,
setAddress(java.net.InetAddress)
public int getPort()
setPort(int)
public byte[] getData()
offset
in the buffer,
and runs for length
long.setData(byte[], int, int)
public int getOffset()
public int getLength()
setLength(int)
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 dataNullPointerException
- if the argument is nullgetData()
,
getOffset()
,
getLength()
public void setAddress(InetAddress iaddr)
iaddr
- the InetAddress
getAddress()
public void setPort(int iport)
iport
- the port numbergetPort()
public void setSocketAddress(SocketAddress address)
address
- the SocketAddress
IllegalArgumentException
- if address is null or is a
SocketAddress subclass not supported by this socketgetSocketAddress()
public SocketAddress getSocketAddress()
SocketAddress
setSocketAddress(java.net.SocketAddress)
public void setData(byte[] buf)
buf
.buf
- the buffer to set for this packet.NullPointerException
- if the argument is null.getLength()
,
getData()
public void setLength(int length)
length
- the length to set for this packet.IllegalArgumentException
- if the length is negative
of if the length is greater than the packet's data buffer
length.getLength()
,
setData(byte[], int, int)
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2024, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.