Listen function Summary: The LISTEN function uses an active connection socket interface to become a connected socket interface, which allows a process to accept requests from other processes, thus becoming a server process. In TCP server programming, the Listen function transforms a process into a server and specifies that the corresponding socket becomes a passive connection. Listen function in general after calling Bind-Called before the accept call, its function prototype is: Intlisten (intSOCKFD,intbacklog) parameter SOCKFD a socket that is listen function, SOCKFD is returned by the socket function. At the time of the socket FD returned by the socket function, it is an active connection socket, that is, the system assumes that the user will call the Connect function on the socket, expect it to actively connect with other processes, and then in the server programming, the user wants this socket can accept the external connection request, That is, passively waiting for the user to connect. Since the system defaults to the fact that a socket is actively connected, it is necessary to tell the system in some way that the user process can do this by invoking the listen system. Parameter backlog This parameter involves some network details. When the process processes a single connection request, there may be other connection requests. Because a TCP connection is a process, there may be a semi-connected state, and sometimes the server process cannot complete a connection request quickly because there are too many users trying to connect at the same time. If this happens, what does the server process want the kernel to do? The kernel maintains a queue in its own process space to track these completed connections, but the server process has not yet taken over the processing or ongoing connection, so a queue kernel cannot be arbitrarily large, so there must be a size limit. This backlog tells the kernel to use this value as the upper limit. There is no doubt that a server process cannot arbitrarily specify a value, and the kernel has a licensed scope. This range is implementation-related. It is difficult to have some kind of unification, generally this value will be less than 30. The Accept Function Summary: Accept () is used to take the parameter s of the socket connection, its function prototype is: Intaccept (intSstructsockaddr * addr,int*Addrlen) The service program calls the Accept function to remove the first customer request from the Client connection request queue of the stream socket s in the listening state, and creates a new socket to create a connection channel with the client socket, and if the connection succeeds, returns the descriptor of the newly created socket, The newly created socket is later exchanged for the data with the client socket, and if it fails, returns Invalid_socket. The first parameter of the function specifies a stream socket in the listening state; the operating system uses the second parameter to return the address structure of the newly created socket, and the operating system uses the third parameter to return the length of the address structure of the newly created socket.
Socket listen/accept