public final class DatagramPacket extends Object
This class can also be used for receiving data from the network.
Note that for all method below where the buffer length passed by the caller cannot exceed the actually length of the byte array passed as the buffer, if this condition is not true, then the method silently reduces the length value to maximum allowable value. Written using on-line Java Platform 1.2 API Specification, as well as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998). Status: Believed complete and correct.
Constructor and Description |
---|
DatagramPacket(byte[] buf,
int length)
Initializes a new instance of
DatagramPacket for receiving
packets from the network. |
DatagramPacket(byte[] buf,
int length,
InetAddress address,
int port)
Initializes a new instance of
DatagramPacket for transmitting
packets across the network. |
DatagramPacket(byte[] buf,
int offset,
int length)
This method initializes a new instance of
DatagramPacket which
has the specified buffer, offset, and length. |
DatagramPacket(byte[] buf,
int offset,
int length,
InetAddress address,
int port)
Initializes a new instance of
DatagramPacket for transmitting
packets across the network. |
DatagramPacket(byte[] buf,
int offset,
int length,
SocketAddress address)
Initializes a new instance of
DatagramPacket for transmitting
packets across the network. |
DatagramPacket(byte[] buf,
int length,
SocketAddress address)
Initializes a new instance of
DatagramPacket for transmitting
packets across the network. |
Modifier and Type | Method and Description |
---|---|
InetAddress |
getAddress()
Returns the address that this packet is being sent to or, if it was used to
receive a packet, the address that is was received from.
|
byte[] |
getData()
Returns the data buffer for this packet
|
int |
getLength()
Returns the length of the data in the buffer
|
int |
getOffset()
This method returns the current offset value into the data buffer where data
will be sent from.
|
int |
getPort()
Returns the port number this packet is being sent to or, if it was used to
receive a packet, the port that it was received from.
|
SocketAddress |
getSocketAddress()
Gets the socket address of the host this packet will be sent to/is coming
from
|
void |
setAddress(InetAddress address)
This sets the address to which the data packet will be transmitted.
|
void |
setData(byte[] buf)
Sets the data buffer for this packet.
|
void |
setData(byte[] buf,
int offset,
int length)
This method sets the data buffer for the packet.
|
void |
setLength(int length)
Sets the length of the data in the buffer.
|
void |
setPort(int port)
This sets the port to which the data packet will be transmitted.
|
void |
setSocketAddress(SocketAddress address)
Sets the address of the remote host this package will be sent
|
public DatagramPacket(byte[] buf, int length)
DatagramPacket
for receiving
packets from the network.buf
- A buffer for storing the returned packet datalength
- The length of the buffer (must be <= buf.length)public DatagramPacket(byte[] buf, int length, InetAddress address, int port)
DatagramPacket
for transmitting
packets across the network.buf
- A buffer containing the data to sendlength
- The length of the buffer (must be <= buf.length)address
- The address to send toport
- The port to send topublic DatagramPacket(byte[] buf, int offset, int length)
DatagramPacket
which
has the specified buffer, offset, and length.buf
- The buffer for holding the incoming datagram.offset
- The offset into the buffer to start writing.length
- The maximum number of bytes to read.public DatagramPacket(byte[] buf, int offset, int length, InetAddress address, int port)
DatagramPacket
for transmitting
packets across the network.buf
- A buffer containing the data to sendoffset
- The offset into the buffer to start writing from.length
- The length of the buffer (must be <= buf.length)address
- The address to send toport
- The port to send topublic DatagramPacket(byte[] buf, int offset, int length, SocketAddress address) throws SocketException
DatagramPacket
for transmitting
packets across the network.buf
- A buffer containing the data to sendoffset
- The offset into the buffer to start writing from.length
- The length of the buffer (must be <= buf.length)address
- The socket address to send toSocketException
- If an error occursIllegalArgumentException
- If address type is not supportedpublic DatagramPacket(byte[] buf, int length, SocketAddress address) throws SocketException
DatagramPacket
for transmitting
packets across the network.buf
- A buffer containing the data to sendlength
- The length of the buffer (must be <= buf.length)address
- The socket address to send toSocketException
- If an error occursIllegalArgumentException
- If address type is not supportedpublic InetAddress getAddress()
null
.public byte[] getData()
public int getLength()
public int getOffset()
public int getPort()
public SocketAddress getSocketAddress()
public void setAddress(InetAddress address)
address
- The destination addresspublic void setData(byte[] buf)
buf
- The new buffer for this packetNullPointerException
- If the argument is nullpublic void setData(byte[] buf, int offset, int length)
buf
- The byte array containing the data for this packet.offset
- The offset into the buffer to start reading data from.length
- The number of bytes of data in the buffer.NullPointerException
- If the argument is nullpublic void setLength(int length)
length
- The new length. (Where len <= buf.length)IllegalArgumentException
- If the length is negative or if the length is greater than the
packet's data buffer lengthpublic void setPort(int port)
port
- The destination portpublic void setSocketAddress(SocketAddress address) throws IllegalArgumentException
address
- The socket address of the remove hostIllegalArgumentException
- If address type is not supported