public final class java.net
DatagramPacket


Show All Login
Java SE 6
  
Details
This class represents a datagram packet.

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.
version1.46, 04/07/06
sinceJDK1.0


Constructors
public DatagramPacket(byte[] buf, int offset, int length) Details
Constructs a 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.
bufbuffer for holding the incoming datagram.
offsetthe offset for the buffer
lengththe number of bytes to read.
since1.2

public DatagramPacket(byte[] buf, int length) Details
Constructs a DatagramPacket for receiving packets of length length.

The length argument must be less than or equal to buf.length.
bufbuffer for holding the incoming datagram.
lengththe number of bytes to read.

public DatagramPacket(byte[] buf, int offset, int length, InetAddress address, int port) Details
Constructs a datagram packet for sending packets of length length with offset ioffsetto the specified port number on the specified host. The length argument must be less than or equal to buf.length.
bufthe packet data.
offsetthe packet data offset.
lengththe packet data length.
addressthe destination address.
portthe destination port number.
since1.2
See also java.net.InetAddress
public DatagramPacket(byte[] buf, int offset, int length, SocketAddress address) throws SocketException Details
Constructs a datagram packet for sending packets of length length with offset ioffsetto the specified port number on the specified host. The length argument must be less than or equal to buf.length.
bufthe packet data.
offsetthe packet data offset.
lengththe packet data length.
addressthe destination socket address.
ThrowsIllegalArgumentException: if address type is not supported
since1.4
See also java.net.InetAddress
public DatagramPacket(byte[] buf, int length, InetAddress address, int port) Details
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.
bufthe packet data.
lengththe packet length.
addressthe destination address.
portthe destination port number.
See also java.net.InetAddress
public DatagramPacket(byte[] buf, int length, SocketAddress address) throws SocketException Details
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.
bufthe packet data.
lengththe packet length.
addressthe destination address.
ThrowsIllegalArgumentException: if address type is not supported
since1.4
See also java.net.InetAddress

Properties
public void setAddress(InetAddress iaddr) Details
Sets the IP address of the machine to which this datagram is being sent.
iaddrthe InetAddress
sinceJDK1.1
See also getAddress()
public InetAddress getAddress() Details
Returns the IP address of the machine to which this datagram is being sent or from which the datagram was received.
returnthe IP address of the machine to which this datagram is being sent or from which the datagram was received.
See also java.net.InetAddress, setAddress(java.net.InetAddress)
public void setData(byte[] buf, int offset, int length) Details
Set the data buffer for this packet. This sets the data, length and offset of the packet.
bufthe buffer to set for this packet
offsetthe offset into the data
lengththe length of the data and/or the length of the buffer used to receive data
ThrowsNullPointerException: if the argument is null
since1.2
See also getData, getOffset, getLength
public void setData(byte[] buf) Details
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.
bufthe buffer to set for this packet.
ThrowsNullPointerException: if the argument is null.
sinceJDK1.1
See also getLength, getData
public byte[] getData() Details
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.
returnthe buffer used to receive or send data
See also setData(byte[], int, int)
public void setLength(int length) Details
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.
lengththe length to set for this packet.
ThrowsIllegalArgumentException: if the length is negative of if the length is greater than the packet's data buffer length.
sinceJDK1.1
See also getLength, setData
public int getLength() Details
Returns the length of the data to be sent or the length of the data received.
returnthe length of the data to be sent or the length of the data received.
See also setLength(int)
public int getOffset() Details
Returns the offset of the data to be sent or the offset of the data received.
returnthe offset of the data to be sent or the offset of the data received.
since1.2
public void setPort(int iport) Details
Sets the port number on the remote host to which this datagram is being sent.
iportthe port number
sinceJDK1.1
See also getPort()
public int getPort() Details
Returns the port number on the remote host to which this datagram is being sent or from which the datagram was received.
returnthe port number on the remote host to which this datagram is being sent or from which the datagram was received.
See also setPort(int)
public void setSocketAddress(SocketAddress address) Details
Sets the SocketAddress (usually IP address + port number) of the remote host to which this datagram is being sent.
addressthe SocketAddress
ThrowsIllegalArgumentException: if address is null or is a SocketAddress subclass not supported by this socket
since1.4
See also getSocketAddress
public SocketAddress getSocketAddress() Details
Gets the SocketAddress (usually IP address + port number) of the remote host that this packet is being sent to or is coming from.
returnthe SocketAddress
since1.4
See also setSocketAddress