Package java.net
Class DatagramPacket
java.lang.Object
java.net.DatagramPacket
public final class DatagramPacket extends Object
This class represents a datagram packet which contains data either to be sent
or received through a
DatagramSocket. It holds additional information
such as its source or destination host.- See Also:
DatagramSocket
-
Constructor Summary
Constructors Constructor Description DatagramPacket(byte[] data, int length)Constructs a newDatagramPacketobject to receive data up tolengthbytes.DatagramPacket(byte[] data, int offset, int length)Constructs a newDatagramPacketobject to receive data up tolengthbytes with a specified buffer offset.DatagramPacket(byte[] data, int offset, int length, InetAddress host, int aPort)Constructs a newDatagramPacketobject to send data to the portaPortof the addresshost.DatagramPacket(byte[] data, int offset, int length, SocketAddress sockAddr)Constructs a newDatagramPacketobject to send data to the addresssockAddr.DatagramPacket(byte[] data, int length, InetAddress host, int port)Constructs a newDatagramPacketobject to send data to the portaPortof the addresshost.DatagramPacket(byte[] data, int length, SocketAddress sockAddr)Constructs a newDatagramPacketobject to send data to the addresssockAddr. -
Method Summary
Modifier and Type Method Description InetAddressgetAddress()Gets the sender or destination IP address of this datagram packet.byte[]getData()Gets the data of this datagram packet.intgetLength()Gets the length of the data stored in this datagram packet.intgetOffset()Gets the offset of the data stored in this datagram packet.intgetPort()Gets the port number of the target or sender host of this datagram packet.SocketAddressgetSocketAddress()Gets the host address and the port to which this datagram packet is sent as aSocketAddressobject.voidresetLengthForReceive()Resets 'length' to the last user-supplied length, ready to receive another packet.voidsetAddress(InetAddress addr)Sets the IP address of the target host.voidsetData(byte[] buf)Sets the data buffer for this datagram packet.voidsetData(byte[] data, int offset, int byteCount)Sets the data buffer for this datagram packet.voidsetLength(int length)Sets the length of the datagram packet.voidsetPort(int aPort)Sets the port number of the target host of this datagram packet.voidsetReceivedLength(int length)Sets 'length' without changing 'userSuppliedLength', after receiving a packet.voidsetSocketAddress(SocketAddress sockAddr)Sets theSocketAddressfor this datagram packet.
-
Constructor Details
-
DatagramPacket
public DatagramPacket(byte[] data, int length)Constructs a newDatagramPacketobject to receive data up tolengthbytes.- Parameters:
data- a byte array to store the read characters.length- the length of the data buffer.
-
DatagramPacket
public DatagramPacket(byte[] data, int offset, int length)Constructs a newDatagramPacketobject to receive data up tolengthbytes with a specified buffer offset.- Parameters:
data- a byte array to store the read characters.offset- the offset of the byte array where the bytes is written.length- the length of the data.
-
DatagramPacket
Constructs a newDatagramPacketobject to send data to the portaPortof the addresshost. Thelengthmust be lesser than or equal to the size ofdata. The firstlengthbytes from the byte array positionoffsetare sent.- Parameters:
data- a byte array which stores the characters to be sent.offset- the offset ofdatawhere to read from.length- the length of data.host- the address of the target host.aPort- the port of the target host.
-
DatagramPacket
Constructs a newDatagramPacketobject to send data to the portaPortof the addresshost. Thelengthmust be lesser than or equal to the size ofdata. The firstlengthbytes are sent.- Parameters:
data- a byte array which stores the characters to be sent.length- the length of data.host- the address of the target host.port- the port of the target host.
-
DatagramPacket
Constructs a newDatagramPacketobject to send data to the addresssockAddr. Thelengthmust be lesser than or equal to the size ofdata. The firstlengthbytes of the data are sent.- Parameters:
data- the byte array to store the data.length- the length of the data.sockAddr- the target host address and port.- Throws:
SocketException- if an error in the underlying protocol occurs.
-
DatagramPacket
public DatagramPacket(byte[] data, int offset, int length, SocketAddress sockAddr) throws SocketExceptionConstructs a newDatagramPacketobject to send data to the addresssockAddr. Thelengthmust be lesser than or equal to the size ofdata. The firstlengthbytes of the data are sent.- Parameters:
data- the byte array to store the data.offset- the offset of the data.length- the length of the data.sockAddr- the target host address and port.- Throws:
SocketException- if an error in the underlying protocol occurs.
-
-
Method Details
-
getAddress
Gets the sender or destination IP address of this datagram packet.- Returns:
- the address from where the datagram was received or to which it is sent.
-
getData
public byte[] getData()Gets the data of this datagram packet.- Returns:
- the received data or the data to be sent.
-
getLength
public int getLength()Gets the length of the data stored in this datagram packet.- Returns:
- the length of the received data or the data to be sent.
-
getOffset
public int getOffset()Gets the offset of the data stored in this datagram packet.- Returns:
- the position of the received data or the data to be sent.
-
getPort
public int getPort()Gets the port number of the target or sender host of this datagram packet.- Returns:
- the port number of the origin or target host.
-
setAddress
Sets the IP address of the target host.- Parameters:
addr- the target host address.
-
setData
public void setData(byte[] data, int offset, int byteCount)Sets the data buffer for this datagram packet. -
setData
public void setData(byte[] buf)Sets the data buffer for this datagram packet. The length of the datagram packet is set to the buffer length.- Parameters:
buf- the buffer to store the data.
-
setLength
public void setLength(int length)Sets the length of the datagram packet. This length plus the offset must be lesser than or equal to the buffer size.- Parameters:
length- the length of this datagram packet.
-
resetLengthForReceive
public void resetLengthForReceive()Resets 'length' to the last user-supplied length, ready to receive another packet. -
setReceivedLength
public void setReceivedLength(int length)Sets 'length' without changing 'userSuppliedLength', after receiving a packet. -
setPort
public void setPort(int aPort)Sets the port number of the target host of this datagram packet.- Parameters:
aPort- the target host port number.
-
getSocketAddress
Gets the host address and the port to which this datagram packet is sent as aSocketAddressobject.- Returns:
- the SocketAddress of the target host.
-
setSocketAddress
Sets theSocketAddressfor this datagram packet.- Parameters:
sockAddr- the SocketAddress of the target host.
-