Question

I have a question about network connection

for instance, A TCP Server support N connections simultaneously, each connection belongs other client host.The question is how many sockets the server needs?

Thanks

Was it helpful?

Solution

I think this is a valid question and do not understand why it has been downvoted.

Before I continue, an important distinction must be made. A socket is a file descriptor, while the port is an "identifier" for a socket. File descriptors/socket are owned by applications, so a port can be viewed as a way to route connections/packets to the correct application.

The way for example a web server works (or any other TCP-based server), is that you have a listen socket that is bound to a port (for example 80). When a client connects to the server, a new socket is automatically created by the operating system (this socket is the one that is returned by for example accept()). This socket is bound to the same local IP and port as the listen socket, but has a different remote IP/port. The operating system stores this mapping and routes packets belonging to this mapping to the new socket.

So the answer to your question is that only one listen socket is needed, but new sockets will be created as clients connect (and removed as they disconnect). The limit of sockets (file descriptors) than an application can create is controlled by the OS.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top