Program Description: multi-process concurrency. When a request is received, fork processes close the connection socket for the parent process, and the sub-process closes the listening socket, each new accept request is executed by the sub-process, and the parent process continues listen.
/*************************************** ************************************ Author: denny wqf363@hotmail.com 2006 * Desc: multi-process concurrency * seat uming: All programs do not need, is garbage, 80% of the time to do 20% of the thing. (Program efficiency) **************************************** * ***********************************/# include <sys/types. h> # include <sys/socket. h> # include <netinet/in. h> # include <ARPA/inet. h> # include <netdb. h> # include <stdio. h> # include <unistd. h> # include <errno. h> # include <string. h> # include <pthread. h> # include <malloc. h> # include <stdlib. h> # define backlog 5 # define maxdatasize 1000 # Define Port 1234 void process_cl I (INT, sockaddr_in); int main (INT argc, char * argv []) // requires a parameter port number {int listenfd, // listen to socket sock connectfd; // new socket struct sockaddr_in servaddr received by accept, // cliaddr server address; // client address socklen_t adrlen; // sockaddr address length int servport; // server listening port number pid_t PID; // obtain the port number of the server listener if (argc! = 2) {printf ("Usage: % s portnumber/A/N", argv [0]); return 1 ;} if (servport = atoi (argv [1]) <0) {printf ("Usage: % s portnumber/A/N", argv [0]); return 1;}/* the server starts to establish the socket descriptor */If (listenfd = socket (af_inet, sock_stream, 0) =-1) {fprintf (stderr, "socket error: % s/n/a", strerror (errno); return 1;}/* set the parameters in the socket address structure servaddr and bind the server port number */servaddr. sin_family = af_inet; servaddr. sin_addr.s_addr = htonl (inaddr_any); servaddr. sin_port = htons (servport); If (BIND (listenfd, (struct sockaddr *) & servaddr, sizeof (servaddr) <0) {printf ("BIND error: % s/n/a ", strerror (errno); exit (2);}/* listen to sockfd descriptor */If (Listen (listenfd, backlog) =-1) {printf ("Listen error: % s/n/a", strerror (errno); exit (3) ;}while (1) {printf ("% s: waiting for data on port tcp % u/N ", argv [0], servport); adrlen = sizeof (struct sockaddr_in); connectfd = accept (listenfd, (struct sockaddr *) & cliaddr, & adrlen); If (connectfd <0) {perror ("accetp () Error/N"); exit (4 );} // use multi-process if (pid = fork ()> 0) {close (connectfd); continue;} else if (pid = 0) {close (listenfd ); process_cli (connectfd, cliaddr); exit (0);} else {printf ("fork () error. /n "); exit (0);} Close (listenfd);} void process_cli (INT connectfd, sockaddr_in client ){//.... close (connectfd );}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.