问题
- Every connection - Server ( unique IP address + host number) + client. This is a unique combination of a connection.
Such combinations can be established based on the number of file descriptors supported by the OS. For instance, my machine has the following limit -
cat /proc/sys/fs/file-max 380594
When we say listen(sock_fd, 5); Here 5 means the number of connections who will not be denied connections. In fact, they will be put into a pending state. Here, let's assume all the fd's are for the socket connection. Then the 389595th till 389599th connection will be put into a pending state. This will be handled once the fd are available. Am I correct on it?
回答1:
Not quite.
Re (1), I don't know what you mean by 'host number'. A client can have two connections to a given server differing only by source port. So to uniquely identify a connection, you need the client IP and port number 2-tuple (pair), and the server IP and port-number 2-tuple.
Re (2), there are several limits at play here. Each open connection requires a file descriptor, but life is not that simple. Connections in TIME_WAIT
for instance may have had their file descriptor closed, but are still a 'connection' from the OS point of view in that they are their precisely to associate stray connection packets with. Further, there may well be an overall limit to the number of files, but there will be other limits. For instance, there is a limit to the number of open fds per process (see getrlimit
). This question has been asked several times before - see What is the theoretical maximum number of open TCP connections that a modern Linux box can have and Increasing the maximum number of tcp/ip connections in linux
Re (3), no, the backlog does not work like that as it is per socket in a listen state. The backlog number indicates the maximum number of TCP connection that the listening socket will acknowledge that have not yet been accepted. When a new TCP connection attempts to connect on a given listening socket, the OS has to queue the existence of that pending connection until it is transferred to a new socket (to handle the particular connection) using accept
. The backlog number is simply the maximum depth of that queue. This has nothing to do with file-max
unless every FD on the system was used by a single process listening on a single socket (a rather unlikely scenario).
来源:https://stackoverflow.com/questions/21656473/is-my-understanding-right-on-the-listen-system-call-on-backlog-and-the-number-of