The accept() system call initiates communications between a connection-oriented server and the client.

int accept(int sockfd, struct sockaddr *cli_addr, int addrlen);

The first parameter is the socket descriptor from the socket() call, sockfd.

The second parameter is the client’s sockaddr address, to be filled in.

The third parameter is the length of the client’s sockaddr structure.

Generally programs call accept() inside an infinite loop, forking a new process for each accepted connection. After accept() returns with client address, the server is ready to accept data.

For example:

for( ; ; ) {
     newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, sizeof(cli_addr));
     if (fork() = 0)  {
          close(sockfd);
										/*
										* read and write data over the network
										* (code missing)
										*/
          exit (0);
     }
     close(newsockfd);
}