Binding and Listening: Port Selection, Backlog Queue, and Bind Address
Binding and Listening: Port Selection, Backlog Queue, and Bind Address
Binding and Listening in Socket Programming
जब हम Socket Programming करते हैं, तो किसी भी Server को Client से connect करने के लिए पहले Binding और Listening process से गुजरना पड़ता है। यह वही process है जो किसी Server को network पर identify करती है और उसे incoming connection accept करने के लिए तैयार करती है।
Binding का मतलब होता है कि हम अपने server को किसी specific IP address और Port number से जोड़ रहे हैं। वहीं Listening का मतलब है कि server अब connection requests को सुनने के लिए ready है।
What is Binding?
Binding का simple मतलब है अपने server socket को एक particular address और port से जोड़ना ताकि network पर उसे पहचाना जा सके। यह काम हम bind() function से करते हैं।
जैसे ही server bind होता है, वह उस port पर आने वाले सभी connections के लिए ready हो जाता है।
Syntax of bind() function (in C/C++)
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
यह function socket को एक local address से जोड़ता है। अगर bind() fail हो जाए, तो इसका मतलब है कि या तो वो port पहले से किसी और process द्वारा use हो रहा है या फिर permission issue है।
Example (in Python)
import socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('127.0.0.1', 8080))
ऊपर दिए example में server_socket को IP address 127.0.0.1 और port 8080 से bind किया गया है।
Port Selection
Port एक logical endpoint होता है जिससे data भेजा या प्राप्त किया जाता है। जब हम server बनाते हैं, तो हमें यह decide करना होता है कि कौन-सा port use करें।
Port selection बहुत important होता है क्योंकि अगर गलत या busy port चुन लिया गया, तो server काम नहीं करेगा। इसलिए port चुनते समय कुछ बातों का ध्यान रखना जरूरी है।
Rules for Port Selection
- 0–1023: ये well-known ports होते हैं जो predefined services के लिए reserved रहते हैं (जैसे 80 for HTTP, 443 for HTTPS, 21 for FTP)।
- 1024–49151: ये registered ports कहलाते हैं और custom applications के लिए use किए जा सकते हैं।
- 49152–65535: ये dynamic or private ports होते हैं, temporary connections के लिए।
आम तौर पर, custom servers 1024 से ऊपर वाले ports पर run किए जाते हैं ताकि conflicts न हों।
Example Table: Common Ports
| Service | Port Number | Protocol |
|---|---|---|
| HTTP | 80 | TCP |
| HTTPS | 443 | TCP |
| FTP | 21 | TCP |
| SSH | 22 | TCP |
| Custom Server | 1024+ | TCP/UDP |
Port selection करते समय ये भी ध्यान रखें कि operating system कुछ ports को block कर सकता है। ऐसे में आपको administrative permission की जरूरत पड़ सकती है।
Bind Address
Bind Address वह IP address होता है जिस पर आपका server socket bind किया जाता है।
अगर आप चाहते हैं कि आपका server सभी interfaces पर listen करे, तो आप address को 0.0.0.0 सेट करते हैं। अगर आप चाहते हैं कि केवल local system पर ही server accessible हो, तो आप 127.0.0.1 या localhost use करेंगे।
Common Bind Address Examples
| Bind Address | Meaning |
|---|---|
| 127.0.0.1 | Server केवल local machine से accessible रहेगा |
| 0.0.0.0 | Server सभी network interfaces पर accessible रहेगा |
| Specific IP | Server सिर्फ उस IP पर listen करेगा |
Example
server_socket.bind(('0.0.0.0', 8080))
इसका मतलब है कि server अब सभी interfaces पर 8080 port पर connections accept करेगा।
Listening in Socket Programming
जब binding complete हो जाती है, तो अगला step होता है Listening। यह step server को instruct करता है कि अब वह incoming connections accept करने के लिए तैयार है।
Listening process listen() function से की जाती है।
Syntax of listen() Function
int listen(int sockfd, int backlog);
यह function बताता है कि server कितनी simultaneous connections को queue में रख सकता है। backlog parameter इसी काम के लिए होता है।
Backlog Queue
Backlog Queue एक temporary waiting area होती है जहाँ incoming client requests store होती हैं, जब तक server उन्हें accept नहीं करता।
जब एक client connect करने की कोशिश करता है, तो उसका request backlog queue में चला जाता है। अगर queue full हो जाए, तो नए connection attempts reject हो जाते हैं।
Example
server_socket.listen(5)
यहाँ 5 backlog value है, जिसका मतलब है कि server एक समय में maximum 5 pending connections रख सकता है।
Backlog Queue Internal Working
- जब client connection request भेजता है, तो वह pending queue में जाता है।
- Server
accept()function call करता है, जिससे queue से एक request उठाई जाती है। - अगर server slow है या backlog छोटी है, तो कुछ connections reject हो सकते हैं।
Connection Lifecycle in Binding and Listening
Server socket के complete connection lifecycle को समझना जरूरी है ताकि आप हर step का सही use कर सकें।
Lifecycle Steps
- Socket Creation: Server एक socket बनाता है (using
socket()). - Binding: Socket को किसी port और address से bind किया जाता है।
- Listening: Server incoming connections सुनने के लिए तैयार होता है।
- Accepting: जब कोई client connect करता है, तो server उस connection को accept करता है।
Example (Full Server Code in Python)
import socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('0.0.0.0', 9090))
server_socket.listen(5)
print("Server listening on port 9090...")
while True:
client_socket, addr = server_socket.accept()
print("Connected with", addr)
client_socket.send(b"Welcome to Server!")
client_socket.close()
ऊपर दिए example में server सभी interfaces पर port 9090 पर listen कर रहा है और एक समय में 5 connections handle कर सकता है।
Importance of Binding and Listening
Binding और Listening किसी भी TCP Server का foundation होते हैं। इनके बिना कोई भी server client से communicate नहीं कर सकता।
Key Points to Remember
- Binding के बिना server का address define नहीं होता।
- Listening के बिना server connection requests accept नहीं कर सकता।
- Backlog का size performance को directly affect करता है।
- Port Selection carefully करना जरूरी है ताकि conflicts न हों।
- Bind Address define करता है कि server accessible कहाँ से होगा।
Practical Tips for Students
- Exam में हमेशा remember करो कि bind() और listen() functions एक साथ use होते हैं।
- Backlog value को बहुत छोटा मत रखो, वरना connections reject हो सकते हैं।
- Port numbers के range और उनके uses को याद रखो (0–1023 reserved)।
- Local testing के लिए हमेशा 127.0.0.1 या localhost use करो।
- Real deployment में 0.0.0.0 helpful रहता है क्योंकि यह सभी network interfaces पर server को उपलब्ध कराता है।
Exam-Oriented Notes (Short Summary)
| Term | Meaning |
|---|---|
| Binding | Server को specific IP और port से जोड़ना |
| Listening | Server का incoming connections सुनने के लिए ready होना |
| Port | Logical endpoint for communication |
| Bind Address | IP address जिस पर server bind होता है |
| Backlog Queue | Pending client connections को temporarily store करने की जगह |
Final Notes
- Binding और Listening network communication के दो सबसे जरूरी steps हैं।
- इनके बिना client-server communication possible नहीं है।
- Exam में coding examples और theory दोनों का balance answer लिखना चाहिए।
- Backlog, Port selection और Bind Address के concepts बार-बार पूछे जाते हैं।