Feedback Form

Sending and Receiving Datagrams: DatagramSocket and DatagramPacket

Sending and Receiving Datagrams: DatagramSocket and DatagramPacket

Introduction

Networking के world में data को भेजने और प्राप्त करने के कई तरीके हैं। उनमें से एक सबसे simple और efficient तरीका है — Datagram communication। इसमें data को छोटे-छोटे packets में तोड़कर भेजा जाता है जिन्हें हम Datagram कहते हैं। Java में Datagram communication को handle करने के लिए दो main classes होती हैं — DatagramSocket और DatagramPacket। यह method connectionless होती है, यानी sender और receiver के बीच कोई permanent connection नहीं बनता।

इस ब्लॉग में हम step by step समझेंगे कि DatagramSocket और DatagramPacket का use करके कैसे data को send और receive किया जाता है। यह topic Java Networking में बहुत important है, खासकर exam point of view से।

What is a Datagram?

Datagram एक independent data unit होता है जो किसी network के through बिना किसी dedicated connection के भेजा जाता है। इसे आप एक postal card की तरह समझ सकते हैं — जिसमें message और destination address दोनों होते हैं। Datagram खुद ही अपने रास्ते से destination तक पहुँचता है।

Datagram communication में अगर कोई packet lost हो जाए, तो उसे दुबारा भेजने की guarantee नहीं होती। इसलिए यह method fast होती है लेकिन unreliable भी हो सकती है।

Key Features of Datagram

  • Connectionless communication — sender और receiver के बीच कोई permanent connection नहीं।
  • Faster transmission — क्योंकि कोई connection setup नहीं होता।
  • Lightweight — TCP की तुलना में कम overhead।
  • No guarantee of delivery — packets lost हो सकते हैं।

DatagramSocket Class

DatagramSocket class का use network पर packets भेजने (send) और प्राप्त करने (receive) के लिए किया जाता है। यह socket layer को represent करता है और communication का main point होता है।

Important Constructors of DatagramSocket

Constructor Description
DatagramSocket() एक socket बनाता है जो किसी भी available port से bind होता है।
DatagramSocket(int port) एक socket बनाता है जो specific port number से bind होता है।
DatagramSocket(int port, InetAddress addr) एक socket बनाता है जो दिए गए port और local address से bind होता है।

Important Methods of DatagramSocket

  • void send(DatagramPacket p) — packet को भेजता है।
  • void receive(DatagramPacket p) — packet को receive करता है।
  • void close() — socket को बंद करता है।
  • int getPort() — socket का port number return करता है।

DatagramPacket Class

DatagramPacket class actual data को represent करता है जो network पर भेजा या प्राप्त किया जाता है। इसे आप एक envelope की तरह समझ सकते हैं जिसमें message और address दोनों मौजूद होते हैं।

Constructors of DatagramPacket

Constructor Use
DatagramPacket(byte[] buf, int length) Receive operation के लिए use होता है।
DatagramPacket(byte[] buf, int length, InetAddress address, int port) Send operation के लिए use होता है।

Important Methods of DatagramPacket

  • byte[] getData() — packet का data return करता है।
  • int getLength() — data की length बताता है।
  • InetAddress getAddress() — destination address return करता है।
  • int getPort() — destination port return करता है।

Sending Data using DatagramSocket

जब sender को data भेजना होता है, तो वह एक DatagramPacket बनाता है जिसमें message, receiver का address और port दिया जाता है। फिर इस packet को DatagramSocket की help से भेजा जाता है।

Example Code (Sender)

import java.net.*; public class UDPSender { public static void main(String[] args) throws Exception { DatagramSocket ds = new DatagramSocket(); String str = "Hello Receiver"; InetAddress ip = InetAddress.getByName("127.0.0.1"); DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(), ip, 3000); ds.send(dp); ds.close(); } }

ऊपर दिए गए example में sender ने “Hello Receiver” message को localhost (127.0.0.1) पर port 3000 पर भेजा है।

Receiving Data using DatagramSocket

Receiver side पर DatagramSocket को उस port पर bind किया जाता है जहाँ sender data भेज रहा है। फिर DatagramPacket के through data receive किया जाता है।

Example Code (Receiver)

import java.net.*; public class UDPReceiver { public static void main(String[] args) throws Exception { DatagramSocket ds = new DatagramSocket(3000); byte[] buf = new byte[1024]; DatagramPacket dp = new DatagramPacket(buf, 1024); ds.receive(dp); String str = new String(dp.getData(), 0, dp.getLength()); System.out.println("Received: " + str); ds.close(); } }

इस example में receiver port 3000 पर data receive करता है और उसे print करता है।

Steps to Send and Receive Datagram

  • Sender पर DatagramSocket object बनाओ।
  • Message को byte array में convert करो।
  • DatagramPacket बनाओ और उसमें message, IP address, और port number डालो।
  • send() method से packet भेजो।
  • Receiver पर DatagramSocket बनाओ और port assign करो।
  • DatagramPacket बनाओ और receive() method से data लो।

Advantages of Datagram Communication

  • Fast communication क्योंकि कोई connection setup नहीं होता।
  • Low overhead क्योंकि acknowledgement की जरूरत नहीं होती।
  • Efficient for small data transmission जैसे games या live streaming।

Disadvantages of Datagram Communication

  • No delivery guarantee — packet lost हो सकता है।
  • No sequencing — packets unordered पहुंच सकते हैं।
  • Not suitable for large data transfer।

Comparison Between TCP and UDP

Feature TCP UDP (Datagram)
Type Connection-oriented Connectionless
Reliability Reliable (acknowledgement) Unreliable (no guarantee)
Speed Slower Faster
Use Case File Transfer, Web Streaming, Gaming

Real Life Applications of DatagramSocket and DatagramPacket

  • Video conferencing और live streaming apps में real-time data transfer।
  • Online games में low latency communication।
  • IoT devices में lightweight message exchange।
  • Broadcast messages भेजने के लिए (जैसे LAN chat apps)।

Important Notes for Exams

  • DatagramSocket — data भेजने और receive करने के लिए use होती है।
  • DatagramPacket — actual data को represent करती है।
  • यह connectionless communication होती है।
  • Methods याद रखो: send(), receive(), close()
  • Example-based question अक्सर आते हैं — sender/receiver program याद रखो।

Summary

DatagramSocket और DatagramPacket Java में UDP communication के base हैं। ये दोनों classes मिलकर connectionless data transfer को possible बनाती हैं। DatagramSocket communication channel की तरह काम करता है जबकि DatagramPacket actual data को hold करता है। इसकी speed fast होती है, लेकिन reliability कम होती है। इसलिए जहां real-time data चाहिए, वहाँ यह best choice होती है।