DatagramSocket in Java: Full UDP Communication Control
DatagramSocket in Java: Full UDP Communication Control
DatagramSocket क्या है?
Java में DatagramSocket class का use UDP (User Datagram Protocol) communication के लिए किया जाता है। यह connectionless protocol है, यानी इसमें data भेजने से पहले connection establish करने की ज़रूरत नहीं होती। Client और Server के बीच data छोटे-छोटे packets के रूप में भेजा जाता है जिन्हें DatagramPackets कहा जाता है।
DatagramSocket basically एक ऐसा communication endpoint है जो network पर packets भेजने और प्राप्त करने का काम करता है।
DatagramSocket की मुख्य विशेषताएँ
- यह connectionless communication provide करता है।
- UDP protocol का उपयोग करता है जो fast और lightweight होता है।
- Packets किसी भी order में पहुँच सकते हैं — delivery guarantee नहीं होती।
- Low latency communication के लिए ideal है।
- Real-time applications जैसे online games, video conferencing में use होता है।
DatagramSocket कैसे काम करता है?
जब हम UDP communication करते हैं, तो दो मुख्य components की आवश्यकता होती है:
- DatagramSocket: यह socket data भेजने और प्राप्त करने का काम करता है।
- DatagramPacket: यह packet actual data को hold करता है जो भेजा या प्राप्त किया जा रहा है।
DatagramSocket किसी particular port number पर bind होता है। जब data भेजा जाता है, तो उसे byte array में convert करके DatagramPacket के अंदर wrap किया जाता है।
DatagramSocket के Constructors
Java में DatagramSocket class के कई constructors available हैं:
| Constructor | Description |
|---|---|
DatagramSocket() |
System द्वारा assign किए गए किसी भी random port पर socket bind करता है। |
DatagramSocket(int port) |
Socket को specific port number पर bind करता है। |
DatagramSocket(int port, InetAddress address) |
Socket को specific IP address और port पर bind करता है। |
DatagramSocket के Important Methods
| Method | Description |
|---|---|
send(DatagramPacket p) |
Packet को network पर भेजने के लिए use किया जाता है। |
receive(DatagramPacket p) |
Incoming packet को receive करता है। यह method blocking होती है। |
close() |
Socket को बंद करता है और resources release करता है। |
getLocalPort() |
Socket का local port number return करता है। |
setSoTimeout(int timeout) |
Receive method के लिए timeout set करता है। |
connect(InetAddress address, int port) |
Socket को किसी specific remote address और port से logically connect करता है। |
disconnect() |
Socket को remote address से disconnect करता है। |
DatagramSocket Example Program
Server Side Code
Server side पर DatagramSocket data receive करता है।
import java.net.*;
public class UDPServer {
public static void main(String[] args) throws Exception {
DatagramSocket serverSocket = new DatagramSocket(9876);
byte[] receiveData = new byte[1024];
System.out.println("Server is running and waiting for client message...");
while (true) {
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
String message = new String(receivePacket.getData(), 0, receivePacket.getLength());
System.out.println("Received from Client: " + message);
InetAddress clientIP = receivePacket.getAddress();
int clientPort = receivePacket.getPort();
String response = "Message received successfully!";
byte[] sendData = response.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, clientIP, clientPort);
serverSocket.send(sendPacket);
}
}
}
Client Side Code
Client side पर DatagramSocket data भेजता है और response receive करता है।
import java.net.*;
import java.util.Scanner;
public class UDPClient {
public static void main(String[] args) throws Exception {
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("localhost");
Scanner sc = new Scanner(System.in);
System.out.print("Enter message: ");
String message = sc.nextLine();
byte[] sendData = message.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendPacket);
byte[] receiveData = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
String response = new String(receivePacket.getData(), 0, receivePacket.getLength());
System.out.println("Response from Server: " + response);
clientSocket.close();
}
}
DatagramSocket vs TCPSocket
TCP और UDP दोनों network communication protocols हैं, लेकिन दोनों की working में काफी फर्क होता है। नीचे table के जरिए difference समझिए:
| Feature | TCP (Socket) | UDP (DatagramSocket) |
|---|---|---|
| Connection Type | Connection-oriented | Connectionless |
| Reliability | Reliable (guaranteed delivery) | Unreliable (no guarantee of delivery) |
| Speed | Comparatively slow | Faster |
| Use Cases | File Transfer, Web Communication | Gaming, Live Streaming, VoIP |
| Error Checking | Yes, in-built | Minimal |
DatagramSocket Buffering Mechanism
DatagramSocket internally data buffering के लिए memory allocate करता है। जब receive() call होती है, तब data buffer में load होता है और DatagramPacket में copy किया जाता है। Buffer size को control करने के लिए setReceiveBufferSize(int size) और setSendBufferSize(int size) methods का use किया जा सकता है।
Error Handling in DatagramSocket
UDP में error handling काफी limited होती है क्योंकि यह connectionless protocol है। फिर भी Java exceptions provide करता है ताकि basic error management किया जा सके। Common exceptions इस प्रकार हैं:
- SocketException: जब socket access या bind fail हो जाए।
- UnknownHostException: जब specified IP address invalid हो।
- IOException: जब data send/receive करते समय I/O error हो।
Real-Life Use Cases of DatagramSocket
- Online Multiplayer Games (fast communication required)
- Live Video Streaming या Voice Chat Apps
- Network Monitoring Tools
- IoT (Internet of Things) devices में lightweight communication
DatagramSocket के Advantages
- Lightweight और fast data transfer करता है।
- No connection overhead — directly packet भेजा जाता है।
- Low latency applications के लिए ideal है।
- Implementation simple और flexible है।
DatagramSocket के Limitations
- Delivery guarantee नहीं होती।
- Packets duplicate या unordered पहुँच सकते हैं।
- Error correction mechanism नहीं होता।
- Large data transmission के लिए suitable नहीं है।
Quick Summary Notes
- Class: java.net.DatagramSocket
- Used For: UDP communication (connectionless)
- Major Methods: send(), receive(), close()
- Packet Class: DatagramPacket
- Protocol: UDP
- Speed: High (but less reliable)
- Applications: Gaming, Streaming, IoT
Exam-Oriented Notes (For Students)
- DatagramSocket UDP protocol पर आधारित है और यह connectionless communication प्रदान करता है।
- DatagramPacket data का actual container होता है जिसे network पर भेजा या प्राप्त किया जाता है।
- UDP में delivery की guarantee नहीं होती, लेकिन यह बहुत fast होता है।
- UDP का use उन applications में किया जाता है जहाँ data loss acceptable है पर speed जरूरी है।
- मुख्य methods हैं —
send(),receive(),close()। - Port number specify करके multiple clients communicate कर सकते हैं।
- UDP का एक बड़ा फायदा यह है कि इसमें connection overhead नहीं होता।