The value returned by accept in socket communication

In the Socket message, when the server accepts the connection, it returns a value that is always greater than 0 if the connection is successful.

 ConnectedSocket = accept(sock_desc, (struct sockaddr *)&echoClntAddr,(socklen_t*)&clntSock);

Consider the client connected to the server and the server with the assigned number 1. Then the client disconnects, and after a while another client connects. Will the accept function assign it number 1 or number 2?

If the accept function assigns number 2, then after how many connections will number 1 be assigned again?

+4
source share
2 answers

The accepted call returns the file descriptor that will be used for the new connection. On the man accept page:

, . -1, errno

1, , ( !). :

accept 2, , 1?

accept . , " " ( ), TCP .

+3

, , accept. accept , , POSIX, POSIX :

open() , .

accept get_unused_fd_flags, __alloc_fd , open FD , , open FD, accept.

, : , accept , .

+1

Source: https://habr.com/ru/post/1609278/


All Articles