Last Updated : 15 Dec, 2021
This class provides facility for connection less transfer of messages from one system to another. Each message is routed only on the basis of information contained within the packet and it may be possible for different packets to route differently. There is also no guarantee as to whether the message will be delivered or not, and they may also arrive out of order. This class provides mechanisms for creation of datagram packets for connectionless delivery using datagram socket class.
Constructors : The constructors vary according to their use, i.e. for receiving or for sending the packet. The important parameters used in these constructors are-
For sending purpose, following constructors are used :
Syntax :public DatagramPacket(byte[] buf, int offset, int length, InetAddress address, int port) Parameters : buf : byte array offset : offset into the array length : length of message to deliver address : address of destination port : port number of destination
Syntax :public DatagramPacket(byte[] buf, int offset, int length, SocketAddress address) Parameters : buf : byte array offset : offset into the array length : length of message to deliver address : socket address of destination
Syntax :public DatagramPacket(byte[] buf, int length, InetAddress address, int port) Parameters : buf : byte array length : length of message to deliver address : address of destination port : port number of destination
Syntax :public DatagramPacket(byte[] buf, int length, SocketAddress address) Parameters : buf : byte array length : length of message to deliver address : socket address of destination
For receiving purpose, following constructors are used :
Syntax :public DatagramPacket(byte[] buf, int offset, int length) Parameters : buf : byte array offset : offset into the array length : length of message to deliver
Syntax :public DatagramPacket(byte[] buf, int length) Parameters : buf : byte array length : length of message to deliver
Methods :
Syntax :public InetAddress getAddress()
Syntax : public int getPort()
Syntax : public byte[] getData()
Syntax : public int getOffset()
Syntax : public int getLength()
Syntax : public void setData(byte[] buf, int offset, int length) Parameters : buf : data buffer offset :offset into the data length : length of the data
Syntax : public void setData(byte[] buf) Parameters : buf : data buffer
Syntax : public void setAddress(InetAddress iaddr) Parameters : iaddr : InetAddress of the recipient
Syntax :public void setPort(int iport) Parameters : iport : the port number
Syntax : public void setSocketAddress(SocketAddress address) Parameters : address : socket address
Syntax : public SocketAddress getSocketAddress()
Syntax :public void setLength(int length) Parameters : length : length of the packet
Java Implementation :
//Java program to illustrate various
//DatagramPacket class methods
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.util.Arrays;
public class datapacket
{
public static void main(String[] args) throws IOException
{
byte ar[] = { 12, 13, 15, 16 };
byte buf[] = { 15, 16, 17, 18, 19 };
InetAddress ip = InetAddress.getByName("localhost");
// DatagramPacket for sending the data
DatagramPacket dp1 = new DatagramPacket(ar, 4, ip, 1052);
// setAddress() method
// I have used same address as in initiation
dp1.setAddress(ip);
// getAddress() method
System.out.println("Address : " + dp1.getAddress());
// setPort() method
dp1.setPort(2525);
// getPort() method
System.out.println("Port : " + dp1.getPort());
// setLength() method
dp1.setLength(4);
// getLength() method
System.out.println("Length : " + dp1.getLength());
// setData() method
dp1.setData(buf);
// getData() method
System.out.println("Data : " + Arrays.toString(dp1.getData()));
// setSocketAddress() method
//dp1.setSocketAddress(address.getLocalSocketAddress());
// getSocketAddress() method
System.out.println("Socket Address : " + dp1.getSocketAddress());
// getOffset() method
System.out.println("Offset : " + dp1.getOffset());
}
}
Output :
Address : localhost/127.0.0.1 Port : 2525 Length : 4 Data : [15, 16, 17, 18, 19] Socket Address : localhost/127.0.0.1:2525 Offset : 0
For a detailed implementation of a client-server program that uses datagram socket to send the actual packets over the network, please refer to - Working with UDP Datagram Sockets
References :
Official Java Documentation
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4