Useful Methods of DatagramPacket Class

1. Constructors

DatagramPacket(byte[] buffer, int bytesToRead)

Creates a DatagramPacket to hold received data. The receive() method of DatagramSocket reads bytesToRead bytes of data and stores in buffer. The bytesToRead must not be greater than buffer.iength else an IiiegaiArgumentException is thrown.

DatagramPacket(byte[] buffer,int bytesToSend,InetAddress address,int port)

Creates a DatagramPacket to hold data to be sent to the specified port number on the specified host. The send() method of DatagramSocket actually sends first bytesToSend bytes of buffer. The bytesToSend must not be greater than buffer.length otherwise an IllegalArgumentException is thrown.

DatagramPacket(byte[] buffer,int offset,int bytesToRead)

Creates a DatagramPacket to hold received data. The received method of DatagramSocket reads bytesToRead bytes of data and places in buffer starting from specified offset. The bytesToRead must not be greater than (buffer.length-offset) else an IllegalArgumentException is thrown.

DatagramPacket(byte[] buffer,int offset,int bytesToSend,InetAddress address, int port)

Creates a DatagramPacket to hold data to be sent to the specified port number on the specified host. The send() method of DatagramSocket actually sends bytesToSend bytes of buffer starting from the specified offset. The bytesToSend must not be greater than (buffer.length-offset) otherwise an IllegalArgumentException is thrown.

DatagramPacket(byte[] buffer,int offset,int bytesToSend, SocketAddress address)

Creates a DatagramPacket to hold data to be sent to the destination datagram socket having specified socket addres. The send() method of DatagramSocket actually sends bytesToSend bytes of buffer starting from the specified offset. The bytesToSend must not be greater than (buffer.length-offset) otherwise an IllegalArgumentException is thrown.

DatagramPacket(byte[] buffer,int bytesToSend,SocketAddress address)

Creates a DatagramPacket to hold data to be sent to the destination datagram socket having specified socket addres. The send() method of DatagramSocket actually sends first bytesToSend bytes of buffer. The bytesToSend must not be greater than buffer.length otherwise an IllegalArgumentException is thrown.

2. Methods

InetAddress getAddress()

Returns the IP address of the host this datagram is destined to or datagram came from.

byte[] getData()

Returns the buffer containing the data received or to be sent. The starting position and size (no of bytes) of the data may be obtained using getOffset() and getLength() methods respectively.

int    getLength ()

Returns the length of the data to be sent or the length of the data received.

int  getOffset()

Returns the offset of the data to be sent or the offset of the data received in the buffer.

int getPort ()

Returns the port number on the remote host to which this datagram is being sent or from which the datagram was received.

SocketAddress getSocketAddress ()

Returns the socket address (IP address and port number) of the remote host that this packet is being sent to or is coming from. The returned object is actually an InetSocketAddress object representing an IP address and a port number pair. Note that InetSocketAddress is a subclass of abstract SocketAddress class. The returned object is first converted to the inetSocketAddress type. To get the address and port, suitable methods on this resultant object may be used.

void setAddress(InetAddress iaddr)

Sets the IP address of the machine to which this datagram is being sent.

void   setData(byte[] buf)

Set the data buffer for this datagram packet.

void   setData(byte[] buf, int offset, int length)

Set the data buffer for this datagram packet. The buffer will contain length bytes of data starting from index offset.

void setLength(int length)

Sets the length of the data in the data buffer for this packet.

void setPort(int iport)

Sets the port number on the remote host to which this datagram is being sent.

void setSocketAddress(SocketAddress address)

Sets the SocketAddress (usually IP address + port number) of the remote host to which this datagram is being

Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.

Leave a Reply

Your email address will not be published. Required fields are marked *