Feedback Form

Constructing Packets: Data Buffer, Offset, Length, Address, and Port

Constructing Packets: Data Buffer, Offset, Length, Address, and Port

Introduction to Packet Construction

जब हम Network Programming में काम करते हैं, तो हमें अक्सर किसी data को network पर भेजना या receive करना होता है। इसके लिए Java में DatagramPacket class का use किया जाता है, जो UDP (User Datagram Protocol) पर आधारित होती है। UDP एक connectionless protocol है, यानी इसमें sender और receiver के बीच कोई direct connection नहीं बनता। इसलिए, data को छोटे-छोटे हिस्सों यानी packets में भेजा जाता है।

इन packets को भेजने से पहले उन्हें सही तरीके से construct करना ज़रूरी होता है — यानि packet में क्या data होगा, कहाँ से शुरू होगा, कितनी लंबाई होगी और किस address या port पर भेजना है — यह सब define करना होता है। यही काम होता है Constructing Packets का।

What is a DatagramPacket?

DatagramPacket एक Java class है जो UDP data को represent करती है। यह packet भेजने और प्राप्त करने दोनों के लिए काम आती है। जब हम packet बनाते हैं, तो हम define करते हैं:

  • कौन सा data भेजना है (Data Buffer)
  • कहाँ से भेजना शुरू करना है (Offset)
  • कितना data भेजना है (Length)
  • किसको भेजना है (Address)
  • किस port number पर भेजना है (Port)

Main Components of Packet Construction

अब step-by-step समझते हैं कि एक packet को construct करने में कौन-कौन से elements होते हैं और उनका क्या role होता है।

1. Data Buffer

Data Buffer एक byte array होती है जिसमें हमारा भेजा जाने वाला actual data store होता है। यानी हम जो भी message या information भेजना चाहते हैं, वह bytes में convert होकर इसी buffer में जाती है।

उदाहरण के लिए:

byte[] buffer = "Hello from Client".getBytes();

यहाँ “Hello from Client” एक string है जिसे byte array में convert किया गया है। यही array हमारा data buffer कहलाएगा।

2. Offset

Offset का मतलब है कि data buffer में data कहाँ से शुरू होगा। Default रूप से यह 0 होता है, यानी शुरुआत से भेजना। लेकिन अगर buffer में कुछ extra data है और हमें बीच से भेजना है, तो offset define किया जा सकता है।

उदाहरण:

int offset = 0;

अगर offset को 5 किया जाए, तो data buffer के पहले 5 bytes skip कर दिए जाएंगे और 6th byte से data भेजा जाएगा।

3. Length

Length यह बताती है कि buffer से कितने bytes भेजने हैं। यह offset के बाद count किया जाता है। यानी अगर buffer में 50 bytes हैं और हमें सिर्फ 20 bytes भेजने हैं, तो length = 20 होगी।

उदाहरण:

int length = 20;

इस तरह हम partial data भी भेज सकते हैं — पूरी buffer भेजने की ज़रूरत नहीं होती।

4. Address

Address उस receiver का IP address होता है जहाँ packet भेजना है। इसे InetAddress class के object के रूप में define किया जाता है।

उदाहरण:

InetAddress address = InetAddress.getByName("localhost");

यहाँ हमने packet को local machine पर भेजने के लिए address "localhost" दिया है। Real-world scenario में यह किसी server का IP हो सकता है जैसे 192.168.1.10

5. Port

Port number उस destination application को represent करता है जो data receive करेगी। हर network application का एक unique port number होता है, जैसे 8080, 5050, आदि।

उदाहरण:

int port = 5000;

Packet Construction Example in Java

अब देखते हैं कि इन सभी components को मिलाकर एक complete DatagramPacket कैसे बनता है।

import java.net.*; public class PacketExample { public static void main(String[] args) throws Exception { // Step 1: Data buffer create करें byte[] data = "Hello Server".getBytes(); // Step 2: Address और Port define करें InetAddress address = InetAddress.getByName("localhost"); int port = 9876; // Step 3: Packet construct करें DatagramPacket packet = new DatagramPacket(data, data.length, address, port); // Step 4: Socket बनाएं और packet भेजें DatagramSocket socket = new DatagramSocket(); socket.send(packet); System.out.println("Packet sent successfully!"); socket.close(); } }

ऊपर दिए गए example में एक client side packet construct किया गया है जो “Hello Server” message UDP के माध्यम से localhost:9876 पर भेजता है।

DatagramPacket Constructors

DatagramPacket class कई तरह के constructors provide करती है ताकि आप विभिन्न तरीके से packet बना सकें:

Constructor Description
DatagramPacket(byte[] buf, int length) Receive करने के लिए empty packet बनाता है।
DatagramPacket(byte[] buf, int offset, int length) Receive packet में offset और length define करता है।
DatagramPacket(byte[] buf, int length, InetAddress address, int port) Send packet के लिए data, address और port define करता है।
DatagramPacket(byte[] buf, int offset, int length, InetAddress address, int port) Send packet में offset के साथ data portion define करता है।

Internal Working of DatagramPacket

जब packet create होता है, तो JVM internal memory में data buffer, offset, length, address और port की जानकारी store करती है। जब socket.send() call किया जाता है, तो ये सारी information underlying network layer तक जाती है और UDP header के साथ encapsulate होकर receiver तक पहुंचती है।

UDP header में source port, destination port, packet length और checksum जैसी details होती हैं, जो packet की integrity verify करने में मदद करती हैं।

Important Methods of DatagramPacket

  • getData() – Returns the data buffer.
  • getOffset() – Returns the starting offset.
  • getLength() – Returns the length of the packet data.
  • getAddress() – Returns destination IP address.
  • getPort() – Returns destination port number.
  • setData(byte[] buf) – Updates the data buffer.
  • setLength(int length) – Updates the length of data to send.

Example: Receiving a Packet

जैसे sender packet भेजता है, वैसे ही receiver को भी packet receive करने के लिए DatagramSocket और DatagramPacket का use करना पड़ता है।

import java.net.*; public class ReceiveExample { public static void main(String[] args) throws Exception { // Receiver socket create करें DatagramSocket socket = new DatagramSocket(9876); // Empty buffer बनाएं receive करने के लिए byte[] buffer = new byte[1024]; // Empty packet बनाएं DatagramPacket packet = new DatagramPacket(buffer, buffer.length); System.out.println("Waiting for packet..."); socket.receive(packet); // Data extract करें String msg = new String(packet.getData(), 0, packet.getLength()); System.out.println("Received: " + msg); socket.close(); } }

ऊपर के code में receiver socket port 9876 पर listen करता है और जैसे ही packet आता है, उसे buffer में store कर लेता है। फिर buffer से data निकालकर print करता है।

Key Points to Remember

  • DatagramPacket class UDP communication के लिए होती है, TCP के लिए नहीं।
  • Packet में data bytes में होना चाहिए, इसलिए String को हमेशा getBytes() से convert करें।
  • Offset और Length आपको partial data भेजने में मदद करते हैं।
  • Always close the socket after sending or receiving packet।
  • UDP में delivery guarantee नहीं होती, इसलिए packet loss possible है।

Practical Usage in Real-World Applications

DatagramPacket का use कई real-world applications में किया जाता है, जैसे:

  • Online games जहां fast response की जरूरत होती है।
  • IoT devices जो lightweight communication करते हैं।
  • Voice/Video Streaming जहां packet loss को tolerate किया जा सकता है।
  • DNS queries जो UDP पर run करती हैं।

Notes for Exam

  • DatagramPacket UDP protocol के लिए use होती है।
  • Data Buffer = byte array जो data store करता है।
  • Offset = data भेजने की starting position।
  • Length = भेजे जाने वाले data की total bytes।
  • Address = receiver का IP address।
  • Port = receiver का port number।
  • Constructors – total 4 types होते हैं send/receive purpose के लिए।
  • UDP connectionless protocol है — fast लेकिन unreliable।
  • getData(), getLength(), getAddress() जैसे methods important हैं।
  • Exam में code-based questions अक्सर आते हैं, जैसे — “Construct a DatagramPacket and send data using DatagramSocket।”