DatagramPacketpublic final class DatagramPacket extends Object This class represents a datagram packet which contains data either to be sent
or received through a {@code DatagramSocket}. It holds additional information
such as its source or destination host. |
Fields Summary |
---|
byte[] | data | int | length | InetAddress | address | int | port | int | offset |
Constructors Summary |
---|
public DatagramPacket(byte[] data, int length)Constructs a new {@code DatagramPacket} object to receive data up to
{@code length} bytes.
this(data, 0, length);
| public DatagramPacket(byte[] data, int length, SocketAddress sockAddr)Constructs a new {@code DatagramPacket} object to send data to the
address {@code sockAddr}. The {@code length} must be lesser than or equal
to the size of {@code data}. The first {@code length} bytes of the data
are sent.
this(data, 0, length);
setSocketAddress(sockAddr);
| public DatagramPacket(byte[] data, int offset, int length, SocketAddress sockAddr)Constructs a new {@code DatagramPacket} object to send data to the
address {@code sockAddr}. The {@code length} must be lesser than or equal
to the size of {@code data}. The first {@code length} bytes of the data
are sent.
this(data, offset, length);
setSocketAddress(sockAddr);
| public DatagramPacket(byte[] data, int offset, int length)Constructs a new {@code DatagramPacket} object to receive data up to
{@code length} bytes with a specified buffer offset.
super();
setData(data, offset, length);
| public DatagramPacket(byte[] data, int offset, int length, InetAddress host, int aPort)Constructs a new {@code DatagramPacket} object to send data to the port
{@code aPort} of the address {@code host}. The {@code length} must be
lesser than or equal to the size of {@code data}. The first {@code
length} bytes from the byte array position {@code offset} are sent.
this(data, offset, length);
setPort(aPort);
address = host;
| public DatagramPacket(byte[] data, int length, InetAddress host, int port)Constructs a new {@code DatagramPacket} object to send data to the port
{@code aPort} of the address {@code host}. The {@code length} must be
lesser than or equal to the size of {@code data}. The first {@code
length} bytes are sent.
this(data, 0, length, host, port);
|
Methods Summary |
---|
public synchronized java.net.InetAddress | getAddress()Gets the sender or destination IP address of this datagram packet.
return address;
| public synchronized byte[] | getData()Gets the data of this datagram packet.
return data;
| public synchronized int | getLength()Gets the length of the data stored in this datagram packet.
return length;
| public synchronized int | getOffset()Gets the offset of the data stored in this datagram packet.
return offset;
| public synchronized int | getPort()Gets the port number of the target or sender host of this datagram
packet.
return port;
| public synchronized java.net.SocketAddress | getSocketAddress()Gets the host address and the port to which this datagram packet is sent
as a {@code SocketAddress} object.
return new InetSocketAddress(getAddress(), getPort());
| public synchronized void | setAddress(java.net.InetAddress addr)Sets the IP address of the target host.
address = addr;
| public synchronized void | setData(byte[] buf, int anOffset, int aLength)Sets the data buffer for this datagram packet.
if (0 > anOffset || anOffset > buf.length || 0 > aLength
|| aLength > buf.length - anOffset) {
throw new IllegalArgumentException(Msg.getString("K002f")); //$NON-NLS-1$
}
data = buf;
offset = anOffset;
length = aLength;
| public synchronized void | setData(byte[] buf)Sets the data buffer for this datagram packet. The length of the datagram
packet is set to the buffer length.
length = buf.length; // This will check for null
data = buf;
offset = 0;
| public synchronized void | setLength(int len)Sets the length of the datagram packet. This length plus the offset must
be lesser than or equal to the buffer size.
if (0 > len || offset + len > data.length) {
throw new IllegalArgumentException(Msg.getString("K002f")); //$NON-NLS-1$
}
length = len;
| public synchronized void | setPort(int aPort)Sets the port number of the target host of this datagram packet.
if (aPort < 0 || aPort > 65535) {
throw new IllegalArgumentException(Msg.getString("K0325", aPort)); //$NON-NLS-1$
}
port = aPort;
| public synchronized void | setSocketAddress(java.net.SocketAddress sockAddr)Sets the {@code SocketAddress} for this datagram packet.
if (!(sockAddr instanceof InetSocketAddress)) {
throw new IllegalArgumentException(Msg.getString(
"K0316", sockAddr == null ? null : sockAddr.getClass())); //$NON-NLS-1$
}
InetSocketAddress inetAddr = (InetSocketAddress) sockAddr;
port = inetAddr.getPort();
address = inetAddr.getAddress();
|
|