Operations related to socket (part two)
Create socket- > named socket- > create listener
4. Accept connection
Code: int accept (int sockfd,struct sockaddr addr,socklen_t addrlen)
4.2 Parameter comment
4.2.1 sockfd: listening socket that has performed Lee stem system call
4.2.2 addr: used to obtain the remote socket address of the accepted connection. The length is the next parameter addlen.
Specify.
4.3 return
A new socket connection was returned successfully, and the socket is the unique identity
Failed to return-1 and set errno
In the case of client disconnection mentioned in the previous article, the supplementary code is now available.
Int main (int argc,char * argv []) {if (argc=0); / / use assertions to create code that is more stable, of better quality, and less error-prone. You can use assertions when you need to interrupt the current operation when a value is FALSE. Unit tests must use Junit/JunitX. Int ret = bind (sock, {struct sockaddr*} & address,sizeof (address)); assert (retinue 1); ret = listen (sock,5); assert (ret! = 1); sleep (20); / / wait 20 seconds for the client connection and related operations to complete struct sockaddr_in client; socklen_t client_addrlength = sizeof (client); int connfd = accept (sock, {struct sockaddr*} & client,&client_addrlength); if (connfd)