Networking in Java: Classes & Interfaces Deep Dive
Networking in Java: Classes & Interfaces Deep Dive
Introduction to Networking in Java
Java में Networking एक बहुत ही powerful concept है जो दो या अधिक systems को आपस में data exchange करने की सुविधा देता है। Simple words में कहें तो Networking का मतलब है — दो devices के बीच data का आदान-प्रदान। Java में यह काम java.net package के अंदर मौजूद classes और interfaces की मदद से किया जाता है।
Java networking का सबसे बड़ा फायदा यह है कि यह platform independent है — यानी किसी भी operating system पर run हो सकता है। इसकी मदद से हम chat applications, web servers, multiplayer games और remote file access जैसी चीज़ें बना सकते हैं।
Basic Concepts of Networking
Networking को समझने के लिए कुछ basic terms को जानना ज़रूरी है — जैसे कि IP Address, Port Number, Socket, Protocols आदि। इन सबका role communication establish करने में होता है।
1. IP Address
IP Address (Internet Protocol Address) एक unique number होता है जो किसी भी device को network पर पहचान देने के लिए उपयोग किया जाता है।
Example — 192.168.1.1. यह दो versions में होता है — IPv4 (32-bit) और IPv6 (128-bit)।
2. Port Number
Port Number किसी specific service या process को identify करने के लिए इस्तेमाल होता है। Example के लिए, HTTP server port 80 पर चलता है और HTTPS port 443 पर।
3. Protocols
Protocols वो नियम होते हैं जिनके अनुसार data transfer किया जाता है। Java में सबसे ज़्यादा use होने वाले protocols हैं:
- TCP (Transmission Control Protocol): Reliable और connection-oriented communication के लिए।
- UDP (User Datagram Protocol): Fast लेकिन connectionless communication के लिए।
4. Socket
Socket एक endpoint होता है जो client और server के बीच communication establish करने में मदद करता है। Client और Server दोनों के पास अपना socket होता है।
Important Java Networking Classes
Java में networking से जुड़ी सारी classes java.net package के अंदर होती हैं।
इनमें से कुछ बहुत ही commonly used classes हैं — जैसे InetAddress, URL, URLConnection,
Socket, ServerSocket, DatagramSocket और DatagramPacket।
1. InetAddress Class
InetAddress class IP address को represent करती है। इसके ज़रिए हम किसी host का IP address या host name निकाल सकते हैं।
InetAddress ip = InetAddress.getByName("www.google.com");
System.out.println(ip.getHostAddress());
System.out.println(ip.getHostName());
ऊपर के example में getByName() method host name से IP address देता है।
getLocalHost() method local machine की जानकारी देता है।
2. URL Class
URL class किसी web resource (जैसे webpage, image, file आदि) को uniquely identify करने के लिए use होती है।
URL url = new URL("https://www.example.com");
System.out.println(url.getProtocol());
System.out.println(url.getHost());
System.out.println(url.getPort());
getProtocol() protocol type बताता है, getHost() domain name देता है, और getPort() port number बताता है।
3. URLConnection Class
URLConnection class किसी URL से connection establish करती है ताकि data send या receive किया जा सके।
URL url = new URL("https://www.example.com");
URLConnection conn = url.openConnection();
InputStream in = conn.getInputStream();
int data = in.read();
while(data != -1){
System.out.print((char)data);
data = in.read();
}
यह code किसी webpage की HTML content को console में print करेगा।
4. Socket and ServerSocket Classes
Socket class client-side communication के लिए और ServerSocket class server-side communication के लिए उपयोग की जाती है।
दोनों classes TCP protocol पर काम करती हैं।
Server Side Example:
ServerSocket ss = new ServerSocket(6666);
Socket s = ss.accept();
DataInputStream dis = new DataInputStream(s.getInputStream());
String msg = dis.readUTF();
System.out.println("Message: " + msg);
ss.close();
Client Side Example:
Socket s = new Socket("localhost", 6666);
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
dout.writeUTF("Hello Server");
dout.flush();
dout.close();
s.close();
यह simple TCP client-server communication example है।
5. DatagramSocket and DatagramPacket Classes
UDP protocol पर communication के लिए DatagramSocket और DatagramPacket classes use होती हैं।
यह connectionless communication provide करती हैं।
UDP Sender (Client) Example:
DatagramSocket ds = new DatagramSocket();
String str = "Hello UDP";
InetAddress ip = InetAddress.getByName("localhost");
DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(), ip, 3000);
ds.send(dp);
ds.close();
UDP Receiver (Server) Example:
DatagramSocket ds = new DatagramSocket(3000);
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, 1024);
ds.receive(dp);
String msg = new String(dp.getData(), 0, dp.getLength());
System.out.println("Received: " + msg);
ds.close();
Important Interfaces in Java Networking
Networking से जुड़े कुछ important interfaces भी Java में मौजूद हैं, जो low-level operations को control करते हैं।
- SocketOptions: यह interface socket options को manage करने के लिए use किया जाता है। जैसे timeout, buffer size आदि।
- ContentHandlerFactory: URL से data के specific types को handle करने के लिए use होता है।
- FileNameMap: यह file name और MIME type के बीच mapping provide करता है।
Java Networking Architecture
Java networking architecture दो हिस्सों में बंटी होती है — Client Side और Server Side। दोनों के बीच connection sockets के ज़रिए बनता है।
| Component | Role |
|---|---|
| Client | Request भेजता है Server को |
| Server | Client के request को process करता है |
| Socket | Communication channel provide करता है |
Advantages of Java Networking
- Platform independent — किसी भी OS पर run हो सकता है।
- Built-in classes और interfaces की वजह से आसान implementation।
- TCP और UDP दोनों प्रकार की communication support करता है।
- High-level abstraction जिससे developers को low-level details की चिंता नहीं करनी पड़ती।
Real-Life Examples of Java Networking
- Chat applications जैसे WhatsApp desktop version।
- Online multiplayer games।
- Web servers और REST APIs।
- File transfer applications।
Exam-Oriented Notes
- Java Networking package — java.net
- Important Classes — InetAddress, URL, URLConnection, Socket, ServerSocket, DatagramSocket, DatagramPacket
- Important Interfaces — SocketOptions, FileNameMap, ContentHandlerFactory
- TCP — Reliable, connection-oriented
- UDP — Fast, connectionless
- IP Address — Device की unique पहचान
- Port Number — Service identifier
- Socket — Client और Server के बीच endpoint
- ServerSocket — Server-side communication establish करता है
- DatagramPacket — UDP packet represent करता है
Quick Summary Table
| Class / Interface | Purpose |
|---|---|
| InetAddress | IP address और host name से related methods |
| URL | Web resource का reference |
| URLConnection | URL से data connection establish करता है |
| Socket | Client-side TCP communication |
| ServerSocket | Server-side TCP communication |
| DatagramSocket | UDP communication provide करता है |
| DatagramPacket | UDP packet represent करता है |