Introduction to Socket: The Endpoint of TCP Communication
Introduction to Socket: The Endpoint of TCP Communication
Socket क्या होता है?
जब हम किसी network में दो devices के बीच communication करवाना चाहते हैं, तो हमें एक ऐसे endpoint की जरूरत होती है जहाँ data भेजा और प्राप्त किया जा सके। इसी endpoint को हम Socket कहते हैं। Simple words में कहें तो, socket वो जगह है जहाँ से communication की शुरुआत और अंत होता है।
Socket एक तरह का interface होता है जो Application Layer और Transport Layer के बीच data transfer को manage करता है। TCP communication में, socket ही वह bridge है जो client और server को जोड़ता है।
Types of Sockets
Java और अन्य programming languages में sockets कई प्रकार के होते हैं। लेकिन TCP communication के लिए दो मुख्य प्रकार के sockets होते हैं:
1. Stream Socket (TCP Socket)
Stream sockets TCP protocol का उपयोग करते हैं। ये connection-oriented होते हैं यानी communication शुरू होने से पहले client और server के बीच proper connection establish किया जाता है। ये data को reliable तरीके से भेजते हैं।
2. Datagram Socket (UDP Socket)
Datagram sockets UDP protocol का उपयोग करते हैं। ये connectionless होते हैं यानी data भेजने से पहले कोई fixed connection नहीं बनाया जाता। ये तेज होते हैं लेकिन data loss का खतरा रहता है।
TCP Communication में Socket की भूमिका
TCP communication एक reliable communication protocol है। यह ensure करता है कि data सही क्रम में और बिना loss के पहुँचे। Socket TCP में इस पूरी process का endpoint होता है — यानी data वहीं से भेजा और प्राप्त किया जाता है।
Client Side Socket
Client socket वह होता है जो connection initiate करता है। Java में, client socket को Socket class के जरिए बनाया जाता है।
Socket s = new Socket("localhost", 8080);
यह code एक client socket बनाएगा जो localhost पर port number 8080 से connect होगा।
Server Side Socket
Server socket वह होता है जो incoming connections को accept करता है। इसे Java में ServerSocket class से बनाया जाता है।
ServerSocket ss = new ServerSocket(8080);
Socket s = ss.accept();
यह code server को port 8080 पर listen करने के लिए तैयार करेगा और client के connection request आने पर उसे accept करेगा।
Socket Communication Process
Socket communication का पूरा process step-by-step इस तरह से होता है:
- Server सबसे पहले एक ServerSocket बनाता है और किसी specific port पर listen करता है।
- Client एक Socket बनाता है और server के IP व port number से connect होता है।
- Connection establish होने के बाद, दोनों तरफ data stream open होती है — एक input stream और एक output stream।
- Data का exchange bidirectional तरीके से होता है।
- Communication पूरा होने पर दोनों sockets close कर दिए जाते हैं।
Important Socket Methods (Java)
| Method | Description |
|---|---|
getInputStream() |
Data receive करने के लिए input stream provide करता है। |
getOutputStream() |
Data भेजने के लिए output stream provide करता है। |
close() |
Socket connection को बंद करता है। |
getInetAddress() |
Connected remote host का IP address देता है। |
getPort() |
Remote port number return करता है। |
Socket Related Classes in Java
Java में socket programming के लिए कई predefined classes होती हैं जो TCP/IP communication को आसान बनाती हैं:
- Socket – Client side communication के लिए।
- ServerSocket – Server side connection के लिए।
- InetAddress – IP address और host name से related operations के लिए।
- InputStream – Data receive करने के लिए।
- OutputStream – Data send करने के लिए।
Example: TCP Communication using Socket in Java
नीचे एक simple example दिया गया है जो client और server के बीच TCP socket communication दिखाता है:
Server Code:
import java.io.*;
import java.net.*;
class Server {
public static void main(String args[]) throws Exception {
ServerSocket ss = new ServerSocket(5000);
Socket s = ss.accept();
DataInputStream dis = new DataInputStream(s.getInputStream());
String str = (String) dis.readUTF();
System.out.println("Message from client: " + str);
ss.close();
}
}
Client Code:
import java.io.*;
import java.net.*;
class Client {
public static void main(String args[]) throws Exception {
Socket s = new Socket("localhost", 5000);
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
dout.writeUTF("Hello Server");
dout.flush();
dout.close();
s.close();
}
}
इस example में client एक message भेजता है और server उसे receive करता है। दोनों के बीच TCP socket के ज़रिए reliable connection बनता है।
Socket और Port Number का संबंध
हर socket किसी specific port number से जुड़ा होता है। Port number यह define करता है कि कौन सा application या process data handle करेगा। TCP और UDP दोनों के लिए port ranges 0 से 65535 तक होती हैं। लेकिन कुछ well-known ports predefined होती हैं जैसे:
| Service | Port Number |
|---|---|
| HTTP | 80 |
| HTTPS | 443 |
| FTP | 21 |
| SMTP | 25 |
| Telnet | 23 |
Socket Lifecycle
Socket का lifecycle चार मुख्य steps में divide किया जा सकता है:
- Creation: Client और server अपने sockets बनाते हैं।
- Connection: Client server से connect होता है।
- Communication: Data input/output streams के ज़रिए exchange होता है।
- Termination: Communication खत्म होने के बाद sockets close हो जाते हैं।
TCP Socket Communication के Advantages
- Reliable data transfer — कोई packet loss नहीं।
- Data सही क्रम में पहुँचता है।
- Error checking और correction available होता है।
- Full-duplex communication (दोनों दिशाओं में data flow)।
TCP Socket Communication के Disadvantages
- Connection establish करने में time लगता है।
- Overhead ज़्यादा होता है (reliability के कारण)।
- Real-time applications के लिए थोड़ा slow।
Socket Programming के Real-life Applications
- Chat applications (जैसे WhatsApp, Messenger)।
- File transfer systems।
- Online games में real-time data exchange।
- Web servers और browsers में communication।
- Remote system monitoring tools।
Quick Summary (Exam Notes)
- Socket: Communication endpoint जो client और server को जोड़ता है।
- TCP Socket: Reliable, connection-oriented communication।
- ServerSocket class: Server-side connections accept करने के लिए।
- Socket class: Client-side connection बनाने के लिए।
- Streams: Data send और receive करने के लिए Input/Output streams का use।
- Ports: Application identification numbers (0–65535)।
- Advantages: Reliable, error-free, ordered delivery।
- Disadvantages: Slow speed, higher overhead।