public final class DatagramPacket extends Object
DatagramSocket. It holds additional information
such as its source or destination host.DatagramSocket| Constructor and Description |
|---|
DatagramPacket(byte[] data,
int length)
Constructs a new
DatagramPacket object to receive data up to
length bytes. |
DatagramPacket(byte[] data,
int length,
InetAddress host,
int port)
Constructs a new
DatagramPacket object to send data to the port
aPort of the address host. |
DatagramPacket(byte[] data,
int offset,
int length)
Constructs a new
DatagramPacket object to receive data up to
length bytes with a specified buffer offset. |
DatagramPacket(byte[] data,
int offset,
int length,
InetAddress host,
int aPort)
Constructs a new
DatagramPacket object to send data to the port
aPort of the address host. |
DatagramPacket(byte[] data,
int offset,
int length,
SocketAddress sockAddr)
Constructs a new
DatagramPacket object to send data to the
address sockAddr. |
DatagramPacket(byte[] data,
int length,
SocketAddress sockAddr)
Constructs a new
DatagramPacket object to send data to the
address sockAddr. |
| Modifier and Type | Method and Description |
|---|---|
InetAddress |
getAddress()
Gets the sender or destination IP address of this datagram packet.
|
byte[] |
getData()
Gets the data of this datagram packet.
|
int |
getLength()
Gets the length of the data stored in this datagram packet.
|
int |
getOffset()
Gets the offset of the data stored in this datagram packet.
|
int |
getPort()
Gets the port number of the target or sender host of this datagram
packet.
|
SocketAddress |
getSocketAddress()
Gets the host address and the port to which this datagram packet is sent
as a
SocketAddress object. |
void |
resetLengthForReceive()
Resets 'length' to the last user-supplied length, ready to receive another packet.
|
void |
setAddress(InetAddress addr)
Sets the IP address of the target host.
|
void |
setData(byte[] buf)
Sets the data buffer for this datagram packet.
|
void |
setData(byte[] data,
int offset,
int byteCount)
Sets the data buffer for this datagram packet.
|
void |
setLength(int length)
Sets the length of the datagram packet.
|
void |
setPort(int aPort)
Sets the port number of the target host of this datagram packet.
|
void |
setReceivedLength(int length)
Sets 'length' without changing 'userSuppliedLength', after receiving a packet.
|
void |
setSocketAddress(SocketAddress sockAddr)
Sets the
SocketAddress for this datagram packet. |
public DatagramPacket(byte[] data,
int length)
DatagramPacket object to receive data up to
length bytes.data - a byte array to store the read characters.length - the length of the data buffer.public DatagramPacket(byte[] data,
int offset,
int length)
DatagramPacket object to receive data up to
length bytes with a specified buffer offset.data - a byte array to store the read characters.offset - the offset of the byte array where the bytes is written.length - the length of the data.public DatagramPacket(byte[] data,
int offset,
int length,
InetAddress host,
int aPort)
DatagramPacket object to send data to the port
aPort of the address host. The length must be
lesser than or equal to the size of data. The first length bytes from the byte array position offset are sent.data - a byte array which stores the characters to be sent.offset - the offset of data where to read from.length - the length of data.host - the address of the target host.aPort - the port of the target host.public DatagramPacket(byte[] data,
int length,
InetAddress host,
int port)
DatagramPacket object to send data to the port
aPort of the address host. The length must be
lesser than or equal to the size of data. The first length bytes are sent.data - a byte array which stores the characters to be sent.length - the length of data.host - the address of the target host.port - the port of the target host.public DatagramPacket(byte[] data,
int length,
SocketAddress sockAddr)
throws SocketException
DatagramPacket object to send data to the
address sockAddr. The length must be lesser than or equal
to the size of data. The first length bytes of the data
are sent.data - the byte array to store the data.length - the length of the data.sockAddr - the target host address and port.SocketException - if an error in the underlying protocol occurs.public DatagramPacket(byte[] data,
int offset,
int length,
SocketAddress sockAddr)
throws SocketException
DatagramPacket object to send data to the
address sockAddr. The length must be lesser than or equal
to the size of data. The first length bytes of the data
are sent.data - the byte array to store the data.offset - the offset of the data.length - the length of the data.sockAddr - the target host address and port.SocketException - if an error in the underlying protocol occurs.public InetAddress getAddress()
public byte[] getData()
public int getLength()
public int getOffset()
public int getPort()
public void setAddress(InetAddress addr)
addr - the target host address.public void setData(byte[] data,
int offset,
int byteCount)
public void setData(byte[] buf)
buf - the buffer to store the data.public void setLength(int length)
length - the length of this datagram packet.public void resetLengthForReceive()
public void setReceivedLength(int length)
public void setPort(int aPort)
aPort - the target host port number.public SocketAddress getSocketAddress()
SocketAddress object.public void setSocketAddress(SocketAddress sockAddr)
SocketAddress for this datagram packet.sockAddr - the SocketAddress of the target host.