Constructor: |
public DatagramPacket(byte[] buf,
int length) {
this (buf, 0, length);
}
Parameters:
buf - buffer for holding the incoming datagram.
length - the number of bytes to read.
|
public DatagramPacket(byte[] buf,
int offset,
int length) {
setData(buf, offset, length);
this.address = null;
this.port = -1;
}
Parameters:
buf - buffer for holding the incoming datagram.
offset - the offset for the buffer
length - the number of bytes to read.
- since:
1.2 -
|
public DatagramPacket(byte[] buf,
int length,
SocketAddress address) throws SocketException {
this(buf, 0, length, 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 - the packet data.
length - the packet length.
address - the destination address.
Throws:
IllegalArgumentException - if address type is not supported
Also see:
- java.net.InetAddress
- since:
1.4 -
|
public DatagramPacket(byte[] buf,
int offset,
int length,
SocketAddress address) throws SocketException {
setData(buf, offset, length);
setSocketAddress(address);
}
Constructs a datagram packet for sending packets of length
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 . Parameters:
buf - the packet data.
offset - the packet data offset.
length - the packet data length.
address - the destination socket address.
Throws:
IllegalArgumentException - if address type is not supported
Also see:
- java.net.InetAddress
- since:
1.4 -
|
public DatagramPacket(byte[] buf,
int length,
InetAddress address,
int port) {
this(buf, 0, length, address, 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 - the packet data.
length - the packet length.
address - the destination address.
port - the destination port number.
Also see:
- java.net.InetAddress
|
public DatagramPacket(byte[] buf,
int offset,
int length,
InetAddress address,
int port) {
setData(buf, offset, length);
setAddress(address);
setPort(port);
}
Constructs a datagram packet for sending packets of length
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 . Parameters:
buf - the packet data.
offset - the packet data offset.
length - the packet data length.
address - the destination address.
port - the destination port number.
Also see:
- java.net.InetAddress
- since:
1.2 -
|
Method from java.net.DatagramPacket Detail: |
public synchronized InetAddress getAddress() {
return address;
}
Returns the IP address of the machine to which this datagram is being
sent or from which the datagram was received. |
public synchronized byte[] getData() {
return buf;
}
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. |
public synchronized int getLength() {
return length;
}
Returns the length of the data to be sent or the length of the
data received. |
public synchronized int getOffset() {
return offset;
}
Returns the offset of the data to be sent or the offset of the
data received. |
public synchronized int getPort() {
return port;
}
Returns the port number on the remote host to which this datagram is
being sent or from which the datagram was received. |
public synchronized SocketAddress getSocketAddress() {
return new InetSocketAddress(getAddress(), getPort());
}
Gets 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 iaddr) {
address = iaddr;
}
Sets the IP address of the machine to which this datagram
is being sent. |
public synchronized void setData(byte[] buf) {
if (buf == null) {
throw new NullPointerException("null packet buffer");
}
this.buf = buf;
this.offset = 0;
this.length = buf.length;
this.bufLength = buf.length;
}
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 . |
public synchronized void setData(byte[] buf,
int offset,
int length) {
/* this will check to see if buf is null */
if (length < 0 || offset < 0 ||
(length + offset) < 0 ||
((length + offset) > buf.length)) {
throw new IllegalArgumentException("illegal length or offset");
}
this.buf = buf;
this.length = length;
this.bufLength = length;
this.offset = offset;
}
Set the data buffer for this packet. This sets the
data, length and offset of the packet. |
public synchronized void setLength(int length) {
if ((length + offset) > buf.length || length < 0 ||
(length + offset) < 0) {
throw new IllegalArgumentException("illegal length");
}
this.length = length;
this.bufLength = this.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 must be lesser or
equal to the offset plus the length of the packet's buffer. |
public synchronized void setPort(int iport) {
if (iport < 0 || iport > 0xFFFF) {
throw new IllegalArgumentException("Port out of range:"+ iport);
}
port = iport;
}
Sets the port number on the remote host to which this datagram
is being sent. |
public synchronized void setSocketAddress(SocketAddress address) {
if (address == null || !(address instanceof InetSocketAddress))
throw new IllegalArgumentException("unsupported address type");
InetSocketAddress addr = (InetSocketAddress) address;
if (addr.isUnresolved())
throw new IllegalArgumentException("unresolved address");
setAddress(addr.getAddress());
setPort(addr.getPort());
}
Sets the SocketAddress (usually IP address + port number) of the remote
host to which this datagram is being sent. |