Class DatagramPacket
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.
 Unless otherwise specified, passing a null argument causes
 a NullPointerException to be thrown.
 
 Methods and constructors of DatagramPacket accept parameters
 of type SocketAddress. DatagramPacket supports
 InetSocketAddress, and may support additional SocketAddress
 sub-types.
- Since:
- 1.0
- 
Constructor SummaryConstructorsConstructorDescriptionDatagramPacket(byte[] buf, int length) Constructs aDatagramPacketfor receiving packets of lengthlength.DatagramPacket(byte[] buf, int offset, int length) Constructs aDatagramPacketfor receiving packets of lengthlength, specifying an offset into the buffer.DatagramPacket(byte[] buf, int offset, int length, InetAddress address, int port) Constructs a datagram packet for sending packets of lengthlengthwith offsetoffsetto the specified port number on the specified host.DatagramPacket(byte[] buf, int offset, int length, SocketAddress address) Constructs a datagram packet for sending packets of lengthlengthwith offsetoffsetto the specified port number on the specified host.DatagramPacket(byte[] buf, int length, InetAddress address, int port) Constructs a datagram packet for sending packets of lengthlengthto the specified port number on the specified host.DatagramPacket(byte[] buf, int length, SocketAddress address) Constructs a datagram packet for sending packets of lengthlengthto the specified port number on the specified host.
- 
Method SummaryModifier and TypeMethodDescriptionReturns the IP address of the machine to which this datagram is being sent or from which the datagram was received, ornullif not set.byte[]getData()Returns the data buffer.intReturns the length of the data to be sent or the length of the data received.intReturns the offset of the data to be sent or the offset of the data received.intgetPort()Returns the port number on the remote host to which this datagram is being sent or from which the datagram was received, or 0 if not set.Returns theSocketAddress(usually IP address + port number) of the remote host that this packet is being sent to or is coming from.voidsetAddress(InetAddress iaddr) Sets the IP address of the machine to which this datagram is being sent.voidsetData(byte[] buf) Set the data buffer for this packet.voidsetData(byte[] buf, int offset, int length) Set the data buffer for this packet.voidsetLength(int length) Set the length for this packet.voidsetPort(int iport) Sets the port number on the remote host to which this datagram is being sent.voidsetSocketAddress(SocketAddress address) Sets the SocketAddress (usually IP address + port number) of the remote host to which this datagram is being sent.
- 
Constructor Details- 
DatagramPacketpublic DatagramPacket(byte[] buf, int offset, int length) Constructs aDatagramPacketfor receiving packets of lengthlength, specifying an offset into the buffer.The lengthargument must be less than or equal tobuf.length.- Parameters:
- buf- buffer for holding the incoming datagram.
- offset- the offset for the buffer
- length- the number of bytes to read.
- Throws:
- IllegalArgumentException- if the length or offset is negative, or if the length plus the offset is greater than the length of the packet's given buffer.
- Since:
- 1.2
 
- 
DatagramPacketpublic DatagramPacket(byte[] buf, int length) Constructs aDatagramPacketfor receiving packets of lengthlength.The lengthargument must be less than or equal tobuf.length.- Parameters:
- buf- buffer for holding the incoming datagram.
- length- the number of bytes to read.
- Throws:
- IllegalArgumentException- if the length is negative or if the length is greater than the length of the packet's given buffer.
 
- 
DatagramPacketConstructs a datagram packet for sending packets of lengthlengthwith offsetoffsetto the specified port number on the specified host. Thelengthargument must be less than or equal tobuf.length.- Parameters:
- buf- the packet data.
- offset- the packet data offset.
- length- the packet data length.
- address- the destination address, or- null.
- port- the destination port number.
- Throws:
- IllegalArgumentException- if the length or offset is negative, or if the length plus the offset is greater than the length of the packet's given buffer, or if the port is out of range.
- Since:
- 1.2
- See Also:
 
- 
DatagramPacketConstructs a datagram packet for sending packets of lengthlengthwith offsetoffsetto the specified port number on the specified host. Thelengthargument must be less than or equal tobuf.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 is null or its type is not supported, or if the length or offset is negative, or if the length plus the offset is greater than the length of the packet's given buffer.
- Since:
- 1.4
- See Also:
 
- 
DatagramPacketConstructs a datagram packet for sending packets of lengthlengthto the specified port number on the specified host. Thelengthargument must be less than or equal tobuf.length.- Parameters:
- buf- the packet data.
- length- the packet length.
- address- the destination address, or- null.
- port- the destination port number.
- Throws:
- IllegalArgumentException- if the length is negative, or if the length is greater than the length of the packet's given buffer, or if the port is out of range.
- See Also:
 
- 
DatagramPacketConstructs a datagram packet for sending packets of lengthlengthto the specified port number on the specified host. Thelengthargument must be less than or equal tobuf.length.- Parameters:
- buf- the packet data.
- length- the packet length.
- address- the destination address.
- Throws:
- IllegalArgumentException- if address is null or its type is not supported, or if the length is negative, or if the length is greater than the length of the packet's given buffer, or if the port is out of range.
- Since:
- 1.4
- See Also:
 
 
- 
- 
Method Details- 
getAddressReturns the IP address of the machine to which this datagram is being sent or from which the datagram was received, ornullif not set.- Returns:
- the IP address of the machine to which this datagram is being sent or from which the datagram was received.
- See Also:
 
- 
getPortpublic int getPort()Returns the port number on the remote host to which this datagram is being sent or from which the datagram was received, or 0 if not set.- Returns:
- the port number on the remote host to which this datagram is being sent or from which the datagram was received.
- See Also:
 
- 
getDatapublic byte[] getData()Returns the data buffer. The data received or the data to be sent starts from theoffsetin the buffer, and runs forlengthlong.- Returns:
- the buffer used to receive or send data
- See Also:
 
- 
getOffsetpublic int getOffset()Returns the offset of the data to be sent or the offset of the data received.- Returns:
- the offset of the data to be sent or the offset of the data received.
- Since:
- 1.2
 
- 
getLengthpublic int getLength()Returns the length of the data to be sent or the length of the data received.- Returns:
- the length of the data to be sent or the length of the data received.
- See Also:
 
- 
setDatapublic void setData(byte[] buf, int offset, int length) Set the data buffer for this packet. This sets the data, length and offset of the packet.- Parameters:
- buf- the buffer to set for this packet
- offset- the offset into the data
- length- the length of the data and/or the length of the buffer used to receive data
- Throws:
- IllegalArgumentException- if the length or offset is negative, or if the length plus the offset is greater than the length of the packet's given buffer.
- Since:
- 1.2
- See Also:
 
- 
setAddressSets the IP address of the machine to which this datagram is being sent.- Parameters:
- iaddr- the- InetAddress, or- null.
- Since:
- 1.1
- See Also:
 
- 
setPortpublic void setPort(int iport) Sets the port number on the remote host to which this datagram is being sent.- Parameters:
- iport- the port number
- Throws:
- IllegalArgumentException- if the port is out of range
- Since:
- 1.1
- See Also:
 
- 
setSocketAddressSets the SocketAddress (usually IP address + port number) of the remote host to which this datagram is being sent.- Parameters:
- address- the- SocketAddress
- Throws:
- IllegalArgumentException- if address is null or is a SocketAddress subclass not supported.
- Since:
- 1.4
- See Also:
 
- 
getSocketAddressReturns theSocketAddress(usually IP address + port number) of the remote host that this packet is being sent to or is coming from.- Returns:
- the SocketAddress
- Since:
- 1.4
- See Also:
 
- 
setDatapublic void setData(byte[] buf) Set the data buffer for this packet. With the offset of this DatagramPacket set to 0, and the length set to the length ofbuf.- Parameters:
- buf- the buffer to set for this packet.
- Since:
- 1.1
- See Also:
 
- 
setLengthpublic void setLength(int 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. Thelengthplus theoffsetmust be lesser or equal to the length of the packet's data buffer.- Parameters:
- length- the length to set for this packet.
- Throws:
- IllegalArgumentException- if the length is negative, or if the length plus the offset is greater than the length of the packet's data buffer.
- Since:
- 1.1
- See Also:
 
 
-