Class DatagramPacket in Java
The DatagramPacket class represents a datagram packet that can be sent or received over the network through DatagramSocket. Datagram packets are used to implement a connectionless packet delivery service.
Each message is routed from one machine to another based solely on the information contained within the packet. Multiple packets sent from a machine to another might be routed differently and can arrive in any order.
One of the DatagramPacket constructors specifies an array of binary data to be sent with its destination address and port. A packet created with this constructor may then be sent with the send() method of a DatagramSocket.
The other DatagramPacket constructor specifies an array of bytes into which data should be received. The receive() method of DatagramSocket waits for data and stores it in a DatagramPacket created in this way. The contents and sender of a received packet may be queried with the DatagramPacket instance methods.
In short, the DatagramPacket class represents a packet of data that can be sent and received over the network using a DatagramSocket. The class is used to implement connectionless data communication.
The structure of the class DatagramPacket is given as:
public final class java.net.DatagramPacket extends java.lang.Object{
//Constructors:
public DatagramPacket(byte iBuff[],int iLength);//constructs a DatagramPacket for a receiving packets of length iLength.The length argument must be or equals to iBuff[] length.
public DatagramPacket(bute iBuff[],int iLength,InetAddress iAddress,int portNumber); //constructs a DatagramPacket for a sending packets of length iLength to the specified port portNumber on the specified host iAddress.The length argument must be or equals to iBuff[] length.
//Methods:
public synchronized InetAddress getAddress();//returns the destination InetAddress.It is used to send data
public synchronized byte[] getData();//returns the byte array of data contained in the datagram. It is used to retrieve
//data from the datagram after it is received.
public synchronized int getLength();//returns the length of the valid data contained in the byte array that would
//be returned from getData() method
public synchronized int getPort();//returns the integer destination port number.It is used to send data
public synchronized void setAddress(InetAddress iaddr);
public synchronized void setData(byte[] ibuf);
public synchronized void setLength(int ilength);
public synchronized void setPort(int iport);
}
The details of the class structure are given as follows:
public DatagramPacket(byte[] ibuf, int ilength);
public DatagramPacket(byte[] ibuf, int ilength) constructor creates a DatagramPacket that receives data. The value of ilength must be less than or equal to ibuf.length. This DatagramPacket can be passed to DatagramSocket.receive().
Parameter
ibuf – The data buffer for receiving incoming bytes.
ilength – The number of bytes to read.
public DatagramPacket(byte[] ibuf, int ilength,InetAddress iaddr, int iport);
public DatagramPacket(byte[] ibuf, int ilength,InetAddress iaddr, int iport) constructor creates a DatagramPacket that sends packets of length ilength to the given port of the specified address. The value of ilength must be less than or equal to ibuf.length. The packets are sent using DatagramSocket.send().
Parameter
ibuf – The data buffer for the packet.
ilength – The number of bytes to send.
iaddr – The destination address.
iport – The destination port number.
public synchronized InetAddress getAddress();
public synchronized InetAddress getAddress() method the address of the machine that sent it if this packet has been received,. If the packet is being sent, the method returns the destination address.
This method returns the IP address of the packet.
public synchronized byte[] getData();
public synchronized byte[] getData() method returns the data buffer associated with this DatagramPacket object. This data is either the data being sent or the data that has been received.
This method returns the packet data.
public synchronized int getLength();
public synchronized int getLength() method returns the length of the message in the buffer associated with this DatagramPacket. This length is either the length of the data being sent or the length of the data that has been received.
This method returns the packet length.
public synchronized int getPort();
public synchronized int getPort() method returns the port number of the machine that sent it if this packet has been received. If the packet is being sent, the method returns the destination port number.
This method returns the port number of the packet.
public synchronized void setAddress(InetAddress iaddr);
public synchronized void setAddress(InetAddress iaddr) method sets the destination address for this packet. When the packet is sent using DatagramSocket.send(), it is sent to the specified address.
Parameter
iaddr – The destination address for the packet.
public synchronized void setData(byte[] ibuf);
public synchronized void setData(byte[] ibuf) method sets the data for this packet. When the packet is sent using DatagramSocket.send(), the specified data is sent.
Parameter
ibuf – The data buffer for the packet.
public synchronized void setLength(int ilength);
public synchronized void setLength(int ilength) method sets the length of the data to be sent for this packet. When the packet is sent using DatagramSocket.send(), the specified amount of data is sent.
Parameter
ilength – The number of bytes to send.
public synchronized void setPort(int iport);
public synchronized void setPort(int iport) method sets the destination port number for this packet. When the packet is sent using DatagramSocket.send(), it is sent to the specified port.
Parameter
iport – The port number for the packet.
Apart from these DatagramPacket class also has inherited methods from class- Object. They are as follows:
- clone()
- finalize()
- hashCode()
- notifyAll()
- wait()
- wait(long, int)
- equals(Object)
- getClass()
- notify()
- toString()
- wait(long)