Structura serverului: fragmentul de cod
int sockfd, newsockfd; if ( (sockfd = socket( ... )) < 0) /* create socket */ ..... if ( bind(sockfd,... ) < 0) /* bind socket */ ..... if ( listen(sockfd,5) < 0) /* announce we're ready */ ..... while (1==1) { /* loop forever */ newsockfd = accept(sockfd, ...); /* wait for client */ if ( (pid = fork()) == 0) { /* child code begins here */ close(sockfd); /* child doesn't wait for client */ ...... /* child does work here, communicating with client using the newsockfd */ ....... exit(0); /* child dies here */ } /* parent continues execution below */ close(newsockfd); /* parent won't communicate with */ /* client - that's childsplay! */ } /* end of while */
Lasă un răspuns