Sending and Receiving: send() and receive() with DatagramSocket
Sending and Receiving: send() और receive() with DatagramSocket in Java
Introduction
अगर आप Java Networking पढ़ रहे हैं, तो DatagramSocket और DatagramPacket एक बहुत ही important concept है, खासकर जब हम UDP (User Datagram Protocol) की बात करते हैं। इस blog में हम step-by-step समझेंगे कि DatagramSocket के साथ data कैसे send() और receive() किया जाता है। यह topic college exams में बहुत बार पूछा जाता है और practical lab में भी frequently use होता है।
UDP एक connectionless protocol है, मतलब इसमें sender और receiver के बीच कोई fixed connection नहीं बनता। Data को छोटे packets में भेजा जाता है जिन्हें Datagram कहा जाता है। अब चलिए समझते हैं कि send() और receive() कैसे काम करते हैं।
What is DatagramSocket and DatagramPacket?
DatagramSocket Java में वो class है जो data को भेजने और प्राप्त करने (sending and receiving) का काम करती है, जबकि DatagramPacket data को contain करने के लिए use होता है। आप DatagramPacket को ऐसे समझ सकते हैं जैसे एक envelope जिसमें data रखा जाता है और DatagramSocket उस envelope को भेजता या receive करता है।
- DatagramSocket: यह actual communication को handle करता है।
- DatagramPacket: यह data, destination address, और port number को hold करता है।
How send() Method Works in DatagramSocket
DatagramSocket class में send() method का use data को भेजने के लिए किया जाता है। यह method DatagramPacket को argument के रूप में लेता है, जिसमें data और destination details होते हैं। जब हम send() call करते हैं, तो packet network के माध्यम से receiver तक पहुंच जाता है।
send() Method Syntax
public void send(DatagramPacket packet) throws IOException
Step-by-Step Explanation
- सबसे पहले एक DatagramSocket object बनाते हैं।
- फिर data को byte array में convert करते हैं।
- फिर एक DatagramPacket बनाते हैं जिसमें data, receiver का IP address और port number दिया जाता है।
- आखिर में send() method से packet को भेजते हैं।
Example of Sending Data
import java.net.*;
public class Sender {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket();
String message = "Hello Receiver";
byte[] buffer = message.getBytes();
InetAddress receiverAddress = InetAddress.getByName("localhost");
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, receiverAddress, 5000);
socket.send(packet);
System.out.println("Message Sent Successfully!");
socket.close();
}
}
ऊपर दिए गए example में हमने localhost पर port 5000 के लिए packet भेजा है। getBytes() method string को bytes में बदल देता है क्योंकि UDP packets byte form में भेजे जाते हैं।
How receive() Method Works in DatagramSocket
Receiving side में receive() method का use किया जाता है जो incoming packet को accept करता है। जब तक कोई packet नहीं आता, यह method block रहता है यानी wait करता है।
receive() Method Syntax
public void receive(DatagramPacket packet) throws IOException
Step-by-Step Explanation
- सबसे पहले एक DatagramSocket बनाते हैं और उसमें port number assign करते हैं।
- फिर एक DatagramPacket बनाते हैं जिसमें खाली byte array दिया जाता है ताकि data receive हो सके।
- receive() method packet को भर देता है जब कोई data आता है।
- फिर packet से message को निकालकर print किया जाता है।
Example of Receiving Data
import java.net.*;
public class Receiver {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(5000);
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
System.out.println("Waiting for data...");
socket.receive(packet);
String message = new String(packet.getData(), 0, packet.getLength());
System.out.println("Received Message: " + message);
socket.close();
}
}
ऊपर के code में receiver port 5000 पर wait कर रहा है। जैसे ही sender packet भेजता है, receive() उसे पकड़ लेता है और data को print करता है।
Flow of Data in UDP Communication
| Step | Description |
|---|---|
| 1 | Sender data को DatagramPacket में रखता है। |
| 2 | Sender DatagramSocket के through packet भेजता है। |
| 3 | Receiver DatagramSocket packet को accept करता है। |
| 4 | Receiver DatagramPacket से data निकालकर read करता है। |
Key Points to Remember
- DatagramSocket और DatagramPacket केवल UDP के लिए use होते हैं।
- UDP connectionless होता है, इसलिए reliability TCP जितनी नहीं होती।
- receive() method blocking call है, यह तब तक रुका रहता है जब तक data नहीं आता।
- send() method non-blocking होता है — packet भेजने के बाद control तुरंत return करता है।
- Port number sender और receiver दोनों में match होना चाहिए।
Internal Working of send() and receive()
जब हम send() call करते हैं, तो JVM उस packet को OS के network stack को pass करता है, जो IP layer पर जाकर destination तक deliver करता है। वहीं, receiver side में OS packet को listen करता है और DatagramSocket को forward करता है, जो receive() method में उसे उपलब्ध कराता है।
UDP में कोई acknowledgment नहीं होती, इसलिए अगर packet lost हो जाए तो resend नहीं होता। यही कारण है कि UDP fast होता है लेकिन reliable नहीं।
Advantages of Using DatagramSocket
- Fast communication क्योंकि इसमें connection setup का time नहीं लगता।
- Lightweight — कम resources use करता है।
- Multicasting या Broadcasting के लिए perfect choice है।
- Real-time apps (जैसे video streaming, gaming) में बहुत useful है।
Limitations of DatagramSocket
- No guarantee कि data सही क्रम में पहुंचेगा।
- Data loss possible है।
- No retransmission in case of packet drop।
Practical Example: Chat Application (Mini Project)
अगर आप DatagramSocket का real-world example समझना चाहते हैं, तो एक छोटा सा chat application बना सकते हैं जहाँ दो users local network पर message exchange करते हैं। हर user sender और receiver दोनों की तरह काम करता है।
Sender and Receiver Combined Example
import java.net.*;
import java.util.Scanner;
public class ChatUser {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
DatagramSocket socket = new DatagramSocket(6000);
InetAddress ip = InetAddress.getByName("localhost");
Thread receiveThread = new Thread(() -> {
try {
while (true) {
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
String message = new String(packet.getData(), 0, packet.getLength());
System.out.println("Friend: " + message);
}
} catch (Exception e) {
e.printStackTrace();
}
});
receiveThread.start();
while (true) {
String msg = sc.nextLine();
byte[] data = msg.getBytes();
DatagramPacket packet = new DatagramPacket(data, data.length, ip, 7000);
socket.send(packet);
}
}
}
यह example दिखाता है कि DatagramSocket का use करके हम simple communication system बना सकते हैं जो बिना TCP connection के काम करता है।
Exam-Oriented Notes
- DatagramSocket UDP communication के लिए use होता है।
- send() method packet को network में भेजता है।
- receive() method incoming packet को accept करता है।
- UDP connectionless protocol है, इसलिए fast लेकिन unreliable है।
- Blocking nature: receive() block रहता है जब तक packet नहीं आता।
- Java में DatagramSocket java.net package का हिस्सा है।
- Sender और Receiver दोनों को specific port number define करना पड़ता है।
- UDP communication में DatagramPacket container का काम करता है।
Summary Table
| Method | Use | Blocking/Non-Blocking |
|---|---|---|
| send() | Packet को भेजने के लिए | Non-Blocking |
| receive() | Incoming packet को receive करने के लिए | Blocking |
Final Note
DatagramSocket और DatagramPacket को समझना Java Network Programming के लिए बहुत ज़रूरी है। exam में अक्सर सवाल आता है — “Explain sending and receiving of data using DatagramSocket” या “Write program to send and receive data using DatagramPacket”। अगर आपने ऊपर के concept और example समझ लिए हैं, तो आप 5 marks वाले सवाल में full marks ले सकते हैं।