• Tech Travel Hub is your one stop, ultimate tour guide to all things tech, travel, visas and digital nomads
  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact
Menu
  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact
View Blog
April 12, 2023April 12, 2023

Class DatagramPacket in Java

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)

Post navigation

Previous post
Next post

Umer Umer

Leave a Reply Cancel reply

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

Recent Posts

  • The Core Element Of A Woocommerce Store And 5 Incredible Plugins
  • 15 Amazing Must-Have WooCommerce Plugins in 2021
  • How to Make A MP4 File Smaller Using Wondershare UniConverter – 5 Important Tips
  • Wondershare Filmii – Create Videos Easily with AI-Powered Video Editor With 4 Important Tips
  • 10 Wonderful Places in India

Categories

  • Adventure
  • Agile Methodology
  • AI
  • All world Tour
  • Android Development
  • Ansible
  • App
  • App Development
  • Artificial Intelligence
  • AWS
  • Bitcoin
  • BlockChain
  • Blogging
  • Box Packing
  • Business
  • business
  • Business Communication
  • buying guide
  • C
  • car
  • Care
  • Certification
  • Certification
  • Chatbot
  • cinema
  • Cloud Tech
  • Content Marketing
  • Corporate
  • covid-19
  • crucible
  • Cryptocurrency
  • Database
  • DevOps
  • DevOps
  • Digital Marketing
  • Drawing
  • Dress
  • Earn Money Online
  • Educational
  • Employment
  • Entertainment
  • Exceptions In Java
  • Fashion
  • Festivals
  • Finance
  • Fitness
  • Food
  • Game
  • Gift ideas
  • Graphic Designers
  • Groovy
  • Health
  • Health
  • home
  • home improvement
  • HR
  • IllegalThreadStateException in Java
  • India Tour
  • Instagram
  • Ios
  • ISO
  • Java
  • Javascript
  • job
  • Kids Section
  • Kitchen
  • Law
  • Life Style
  • Lifestyle
  • Lifestyle Home Improvement
  • Magento
  • Main Stories
  • Management
  • Mattress
  • Motorable
  • Movie
  • nursing
  • Office Productivity
  • Pendrive
  • Perspective
  • Pet
  • Popular
  • Programming
  • Python
  • RDT
  • Relationships
  • Selenium
  • SEO
  • Skin
  • SOA
  • SOAPUI
  • Social Media
  • Social Media Marketing
  • Software
  • software development
  • Software Development Model
  • Software Engineering
  • Software Testing
  • Sports
  • Startup
  • T Robot
  • TDD
  • Tech
  • Tech News
  • Tech News
  • Technology
  • Tool
  • Tour Guide
  • Tour Guide
  • Travel
  • UFT
  • Uncategorized
  • USB Cable
  • VBscript
  • VPN
  • Web Development
  • WebHosting
  • WebServices
  • Wildlife
  • Wordpress
  • Yoga
  • Youtube

Tags

Instagram Instagram Marketing Marketing SEO Thoptv App

Categories

  • Tech
  • Travel
  • Visas
  • Digital Nomad
  • Tech
  • Travel
  • Visas
  • Digital Nomad

Publishing

If you would like to publish on our site, please email admin@techtravelhub.com

Quick Links

  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact
  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact

Contact

Tech Travel Hub LLC

Address:
8 The Green STE A
Dover
Delaware, 19901

Phone: +1 302 956 9948