O código do servidor chama accept()
apenas uma vez. Assim, somente a primeira tentativa de conexão é efetivamente aceita e as conexões de clientes restantes são mantidas em uma fila de pedidos de conexão que vive no espaço do kernel. A próxima conexão do cliente será recuperada da fila quando accept()
for chamado novamente.
Nenhum processo possui conexões de cliente enquanto elas permanecem no espaço do kernel, porque vários processos ou encadeamentos podem aceitar legalmente conexões de um par exclusivo de endereço e porta se habilitarem a opção SO_REUSEPORT
em todos os descritores de soquete participantes.
Você pode testar a opção SO_REUSEPORT
sozinho adicionando o seguinte snippet de código antes da chamada bind()
e executando mais de um servidor. Você vai descobrir que o kernel irá distribuir pedidos entre eles.
{
int enabled = -1;
if (setsockopt (sockd, SOL_SOCKET, SO_REUSEPORT,
(void*) &enabled, sizeof (enabled)) < 0) {
perror ("setsockopt");
}
}
Referência de man 2 accept
:
The accept(sockfd) system call is used with connection-based socket types (SOCK_STREAM, SOCK_SEQPACKET). 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.
Referência de man 7 socket
:
SO_REUSEPORT (since Linux 3.9)
Permits multiple AF_INET or AF_INET6 sockets to be bound to an identical socket address. This option must be set on each socket (including the first socket) prior to calling bind(2) on the socket. To prevent port hijacking, all of the processes binding to the same address must have the same effective UID. This option can be employed with both TCP and UDP sockets.
For TCP sockets, this option allows accept(2) load distribution in a multi-threaded server to be improved by using a distinct listener socket for each thread. This provides improved load distribution as compared to traditional techniques such using a single accept(2)ing thread that distributes connections, or having multiple threads that compete to accept(2) from the same socket.
For UDP sockets, the use of this option can provide better distribution of incoming datagrams to multiple processes (or threads) as compared to the traditional technique of having multiple processes compete to receive datagrams on the same socket.