Feedback Form

Creating and Connecting Sockets: Host, Port, and Timeout Configuration

Creating and Connecting Sockets: Host, Port, and Timeout Configuration

Socket क्या है और इसे क्यों बनाया जाता है?

जब भी दो devices या programs के बीच communication करना होता है, तो हमें एक medium चाहिए जिससे data transfer हो सके। यही काम Socket करता है। Socket एक endpoint होता है जो network communication के लिए use होता है — यानी sender और receiver के बीच data भेजने और प्राप्त करने का रास्ता बनाता है।

Java, Python, C जैसी languages में socket का use बहुत common है। उदाहरण के लिए, जब आप किसी web server से connect करते हैं, तो आपका browser socket create करके उस server के साथ communication शुरू करता है।

Simple definition:

Socket एक bridge की तरह होता है जो client और server को जोड़ता है ताकि वे message या data exchange कर सकें।

Socket के प्रकार (Types of Sockets)

मुख्य रूप से दो प्रकार के sockets होते हैं —

  • Stream Socket (TCP) – Reliable, connection-oriented communication के लिए।
  • Datagram Socket (UDP) – Fast, connectionless communication के लिए।

Stream sockets data को proper sequence में भेजते हैं और confirm करते हैं कि data सही पहुंचा या नहीं। वहीं Datagram sockets बस data भेज देते हैं, confirmation नहीं लेते।

Socket Creation कैसे होता है?

Socket बनाने के लिए programming language के specific methods या classes का use किया जाता है। उदाहरण के लिए Java में:

Socket s = new Socket("hostname", portNumber);

ऊपर दिए गए code में “hostname” उस server का नाम या IP address है जिससे connect होना है, और “portNumber” वह gateway है जहाँ connection establish होगा।

Python Example:

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("localhost", 8080))

इस example में Python का socket module use किया गया है। यहाँ AF_INET का मतलब है IPv4 address family, और SOCK_STREAM TCP connection को दर्शाता है।

Host और Port क्या होते हैं?

Host:

Host किसी device का network identity होता है। यह IP address (जैसे 192.168.1.1) या domain name (जैसे www.google.com) के रूप में हो सकता है। जब आप किसी server से connect करते हैं, तो आपको यह बताना होता है कि किस host से connection बनाना है।

Port:

Port एक logical channel होता है जो किसी application या process को uniquely identify करता है। हर server पर कई ports होते हैं, जैसे:

  • Port 80 – HTTP (Web Traffic)
  • Port 443 – HTTPS (Secure Web Traffic)
  • Port 25 – SMTP (Email Sending)

जब client किसी specific service से जुड़ना चाहता है, तो वह उसी port को target करता है।

Port Allocation कैसे होती है?

Port numbers 0 से 65535 तक होते हैं, और इन्हें तीन categories में बांटा गया है:

Port Range Type Use
0 – 1023 Well-known Ports HTTP, FTP, SMTP जैसे standard services के लिए
1024 – 49151 Registered Ports Custom या specific applications के लिए
49152 – 65535 Dynamic/Private Ports Temporary या client-side connections के लिए

Client और Server Socket में फर्क

Socket communication में हमेशा दो पक्ष होते हैं — एक server और दूसरा client।

  • Server Socket: यह हमेशा किसी fixed port पर listen करता है। जैसे ही कोई client connect करने की कोशिश करता है, server उसे accept करता है।
  • Client Socket: यह server के host और port को target करके connection initiate करता है।

Java Example:

Server Side:

ServerSocket ss = new ServerSocket(8080);
Socket s = ss.accept();
System.out.println("Client connected!");

Client Side:

Socket s = new Socket("localhost", 8080);
System.out.println("Connected to Server!");

इस तरह client और server के बीच connection establish हो जाता है।

Timeout Configuration क्या है?

Timeout का मतलब है — connection या data transmission के लिए एक time limit fix करना। अगर उस time के अंदर response नहीं मिलता, तो socket connection automatically बंद हो जाता है। यह system को hang या infinite wait से बचाता है।

Java में Timeout सेट करना:

Socket s = new Socket();
s.connect(new InetSocketAddress("localhost", 8080), 5000);

यहाँ 5000 milliseconds (यानि 5 seconds) का timeout दिया गया है। अगर 5 सेकंड में connection नहीं बनता, तो exception throw हो जाता है।

Python में Timeout सेट करना:

s = socket.socket()
s.settimeout(10)
s.connect(("localhost", 8080))

यह code 10 seconds तक wait करेगा, उसके बाद connection timeout error देगा।

Connection Process Step-by-Step

Socket connection बनने की पूरी प्रक्रिया इस प्रकार होती है:

  • Step 1: Server अपने port पर listen करता है।
  • Step 2: Client host और port के माध्यम से request भेजता है।
  • Step 3: Server request को accept करता है।
  • Step 4: Connection establish होता है और data transfer शुरू होता है।
  • Step 5: Communication पूरा होने पर connection close किया जाता है।

Data Transfer कैसे होता है?

Socket के जरिए data byte form में भेजा और प्राप्त किया जाता है। जब client data भेजता है, तो वह socket के output stream से जाता है और server उसे input stream से receive करता है।

Example (Java):

OutputStream os = s.getOutputStream();
os.write("Hello Server".getBytes());
InputStream is = s.getInputStream();
byte[] data = new byte[100];
is.read(data);
System.out.println(new String(data));

ऊपर दिए code में client “Hello Server” message भेजता है और server का response receive करता है।

Socket Configuration Parameters

Socket को configure करने के लिए कई important parameters होते हैं:

Parameter Description
Host Server का IP address या domain name
Port Connection के लिए specific gateway
Timeout Maximum wait time for connection or data transfer
Protocol TCP या UDP
Buffer Size Data packet size जो एक बार में भेजा या प्राप्त किया जा सकता है

Common Socket Errors और उनके Solutions

  • Connection Refused: Server चालू नहीं है या port block है।
  • Timeout Error: Connection time limit cross हो गई है। Timeout बढ़ाकर या network check करके fix किया जा सकता है।
  • Broken Pipe: Connection अचानक बंद हो गया है। Proper close handling जरूरी है।
  • Unknown Host: Hostname गलत है या resolve नहीं हो पा रहा।

Best Practices for Socket Programming

  • Always close socket after communication using s.close().
  • Proper timeout set करें ताकि program infinite wait में न जाए।
  • Data encryption का use करें जब sensitive information भेजनी हो।
  • Thread या asynchronous handling से multiple clients manage करें।
  • Exceptions को handle करें ताकि system crash न हो।

Real-World Example

मान लो आप एक Chat Application बना रहे हैं।

  • Server एक fixed port (जैसे 8080) पर listen करेगा।
  • हर client socket बनाकर उस port से connect होगा।
  • Client message भेजेगा और server उसे दूसरे clients तक broadcast करेगा।

यही basic socket communication का real-world use case है।

Quick Summary (Exam Notes)

  • Socket: Network communication का endpoint।
  • Host: Server का address (IP या Domain)।
  • Port: Specific service का channel number।
  • Timeout: Connection wait time।
  • TCP Socket: Reliable, ordered connection।
  • UDP Socket: Fast, connectionless communication।
  • ServerSocket: Requests accept करने के लिए।
  • Socket: Connection initiate करने के लिए।
  • Best Practice: Always close socket और proper timeout set करें।