Creating TCP Clients: Socket, Timeouts, and Keep-Alive Configuration
Creating TCP Clients: Socket, Timeouts, and Keep-Alive Configuration
TCP Client क्या होता है?
TCP (Transmission Control Protocol) Client एक ऐसा program होता है जो किसी server से connection establish करके data exchange करता है। जब भी हम internet पर कोई data send या receive करते हैं — जैसे email भेजना, file download करना या web request करना — तो उसके पीछे TCP client और TCP server communication होता है।
TCP Client का main काम होता है connection बनाना, data भेजना, और फिर connection को बंद करना। यह reliable communication देता है, यानी data loss नहीं होता क्योंकि TCP packet acknowledgment और retransmission technique use करता है।
Socket Programming क्या है?
Socket programming एक technique है जिससे हम network communication कर सकते हैं। यह client और server के बीच data transfer के लिए endpoint की तरह काम करता है। Java, Python, C जैसी languages में socket के जरिए हम TCP या UDP connections बना सकते हैं।
Socket क्या होता है?
Socket एक communication endpoint होता है। जब TCP client server से जुड़ता है, तो दोनों के बीच दो socket बनते हैं — एक client-side socket और एक server-side socket।
- Client Socket: यह server से connection initiate करता है।
- Server Socket: यह client requests को accept करता है।
Java में socket class का use करके TCP client बनाया जाता है।
Socket socket = new Socket("localhost", 8080);
ऊपर दिए गए code में “localhost” server का address है और “8080” port number है जिस पर connection बनाया जा रहा है।
TCP Client कैसे काम करता है?
TCP client का working process step-by-step इस तरह होता है:
- Step 1: Server का IP और port define किया जाता है।
- Step 2: Socket class से connection establish किया जाता है।
- Step 3: OutputStream के जरिए data भेजा जाता है।
- Step 4: InputStream से data receive किया जाता है।
- Step 5: Communication complete होने के बाद socket close किया जाता है।
इस तरह client और server के बीच data का सुरक्षित और ordered exchange होता है।
TCP Client का Java Example
नीचे एक simple Java TCP Client का example दिया गया है जो server से जुड़कर message send करता है और reply receive करता है।
import java.io.*;
import java.net.*;
public class SimpleTCPClient {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 5000);
DataOutputStream dout = new DataOutputStream(socket.getOutputStream());
dout.writeUTF("Hello Server");
DataInputStream din = new DataInputStream(socket.getInputStream());
String response = din.readUTF();
System.out.println("Server says: " + response);
dout.close();
din.close();
socket.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
यह code client को server से जोड़ता है और data exchange करता है। यह college exam में बहुत important program है।
Timeout Configuration क्या है?
Timeout का मतलब है — कितनी देर तक client connection या response का इंतजार करेगा। अगर server समय पर reply नहीं देता, तो timeout error आता है। इससे client infinite wait से बच जाता है।
Types of Timeouts
- Connection Timeout: यह बताता है कि client connection establish करने के लिए कितना समय तक इंतजार करेगा।
- Read Timeout: यह बताता है कि client server से data पढ़ने के लिए कितना इंतजार करेगा।
Java में Timeout Set करना
Socket socket = new Socket();
socket.connect(new InetSocketAddress("localhost", 5000), 3000); // Connection timeout 3 sec
socket.setSoTimeout(5000); // Read timeout 5 sec
ऊपर दिए गए example में अगर server 3 second के अंदर connect नहीं होता, तो SocketTimeoutException आती है।
Keep-Alive Configuration क्या है?
Keep-Alive feature TCP connection को लंबे समय तक active रखता है ताकि बार-बार नया connection न बनाना पड़े। यह performance improve करता है, खासकर जब multiple requests continuously भेजी जा रही हों।
Keep-Alive कैसे Enable करें?
Java socket में Keep-Alive enable करने के लिए:
Socket socket = new Socket("localhost", 8080);
socket.setKeepAlive(true);
यह command connection को idle state में भी alive रखता है ताकि बार-बार connection reopen न करना पड़े।
Keep-Alive कैसे काम करता है?
जब client और server के बीच कुछ समय तक कोई data transfer नहीं होता, तो TCP periodic messages भेजकर check करता है कि connection अभी भी valid है या नहीं। अगर server respond नहीं करता, तो connection close कर दिया जाता है।
TCP Configuration Summary Table
| Setting | Method | Description |
|---|---|---|
| Connection Timeout | connect(address, timeout) |
Connection establish होने का समय limit करता है |
| Read Timeout | setSoTimeout(ms) |
Server response के इंतजार का समय limit करता है |
| Keep-Alive | setKeepAlive(true) |
Idle connections को active रखता है |
TCP Client के फायदे
- Reliable data transfer — हर packet acknowledge होता है।
- Ordered delivery — data सही sequence में पहुंचता है।
- Error control — TCP automatic retransmission करता है।
- Flow control — sender और receiver की speed balance करता है।
TCP Client की Limitations
- Connection-oriented होने से थोड़ा slow होता है।
- Extra header और acknowledgment के कारण overhead बढ़ जाता है।
- Real-time apps (जैसे gaming) के लिए UDP बेहतर होता है।
Practical Implementation Example
अगर आप एक chat application बना रहे हैं, तो TCP client हर message भेजने के लिए socket का use करेगा। उदाहरण के लिए — WhatsApp और Email जैसी apps TCP-based होती हैं क्योंकि इनमे reliability जरूरी होती है।
नीचे एक simple TCP chat example का idea देखें:
Socket client = new Socket("127.0.0.1", 7000);
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(client.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
System.out.println("Connected to Server");
String msg;
while(true){
System.out.print("You: ");
msg = userInput.readLine();
out.println(msg);
if(msg.equalsIgnoreCase("bye")) break;
System.out.println("Server: " + in.readLine());
}
client.close();
यह simple code communication को interactive बना देता है और conceptually समझने में आसान है।
TCP Client Best Practices
- Always set timeouts ताकि infinite waiting से बचा जा सके।
- Exception handling ज़रूरी है ताकि crash न हो।
- Socket close करना न भूलें, वरना resources leak हो सकते हैं।
- Keep-Alive enable करें अगर continuous communication है।
- Buffered streams का use करें performance के लिए।
Real-World Usage
TCP clients का use हर जगह होता है — web browsers, chat applications, file transfer tools, IoT devices, banking software आदि में। जहां भी reliable communication चाहिए, वहां TCP prefer किया जाता है।
Exam-Oriented Notes
- TCP Client connection बनाने के लिए
Socketclass use होती है। - Server से data भेजने के लिए
OutputStreamऔर receive करने के लिएInputStreamuse होता है। - Timeout connection और response waiting limit करता है।
- Keep-Alive TCP connection को active रखता है।
SocketTimeoutExceptionतब आती है जब server समय पर respond नहीं करता।- TCP reliable और ordered data transfer protocol है।
- Java में
setKeepAlive(true)से connection को reuse किया जा सकता है। - Client और Server दोनों में proper resource closing जरूरी है।