A port can only be used by one program, so why can multiple client in tcp connect to the same port of a server?

such as the question, is there something wrong with my understanding? Ask for advice.

Mar.28,2021

does not understand where your point is, and your questions do not conflict with the principles described in the previous paragraph.
the server is multi-process or multi-threaded, or uses a proxy, and so on. If a link comes in, it will be processed by a child process / thread / a web server, and the next request will not be affected.


guess that the subject may have confused the concepts of socket and port.

for example, port is the house number. Your family lives in. If you don't move out, the next one can't enter. Socket is the courier number, and you can send several couriers to your family at the same time. Port simply identifies a process in the network, and socket is the endpoint of data exchange.

excerpt from man accept , which translates into Chinese as follows

It extracts the first connection request on the queue of pending connections for the listening socket, sockfd , creates a new connected socket, and returns a new file descriptor referring to that socket. The newly created socket is not in the listening state. The original socket sockfd is unaffected by this call.

it (accept) takes the first request from the queue of the listening socket sockfd , creates a new connection socket , and returns a file descriptor that points to the new socket. The new socket is not listening. The original socket sockfd will not be changed by this call.

so it has nothing to do with "switching fast enough", "sub-process / thread" or "Nginx configuration". They are just subsequent methods to deal with these new socket, whether to deal with them one by one or together, which is equivalent to your express delivery can be received one by one, or you can ask the courier to send it up with a cart. But the ability to support multiple connections is essentially because each client connection has a new socket, that is irrelevant and can be handled separately.

the system distinguishes socket, by quintuple. Because both IP are immutable, applications can only be distinguished by port numbers, so a port is required to be used by only one program.


< hr >

throw a brick to attract jade

  • A server has installed Nginx and started listening to the 80 port, but then you download another Apache to listen to port 80 if you want to start it. At this time, the server will not give way and will prompt that the port is occupied. This is a port corresponds to an application .
  • when you visit a URL, the 80 port will be accessed by default. Suppose the server uses Nginx . When Nginx listens to the 80 port that a customer requests to monitor, it will make a corresponding response according to the request. As for why multiple users can be linked at the same time, it depends on the configuration of the server itself, and how many users are allowed to access at the same time. If only one is allowed, then the first one comes in. The next one will be queued in sequence. After the server processes one, it will then proceed to the next processing
  • .

computer principle: linux multi-user is actually a single user, but if you switch fast enough, you feel like you are using

concurrently.
Menu