In linux, the parent-child process sharing port nginx uses the parent-child process sharing port, so nginx is fast. this is also a reason # include & lt; unistd. h & gt; # include & lt; stdlib. h & gt; # include & lt; sys/types. h & gt; # include & lt; stdio. h & gt; # include & lt linux parent-child process sharing port nginx uses the parent-child process sharing port, so nginx is fast. this is also a reason # include # Include # Include # Include # Include # Include # Include Int main () {int sfp, nfp; struct sockaddr_in s_add, c_add; int sin_size; unsigned short portnum = 8888; printf ("Hello, welcome to my server! \ R \ n "); sfp = socket (AF_INET, SOCK_STREAM, 0); if (-1 = sfp) {printf (" socket fail! \ R \ n "); return-1;} printf (" socket OK! \ R \ n "); bzero (& s_add, sizeof (struct sockaddr_in); s_add.sin_family = AF_INET; region = htonl (INADDR_ANY); s_add.sin_port = htons (portnum ); if (-1 = bind (sfp, (struct sockaddr *) (& s_add), sizeof (struct sockaddr) {printf ("bind fail! \ R \ n "); return-1;} printf (" bind OK! \ R \ n "); if (-1 = listen (sfp, 5) {printf (" listen fail! \ R \ n "); return-1;} printf (" listen OK \ r \ n "); fork (); while (1) {sin_size = sizeof (struct sockaddr_in); nfp = accept (sfp, (struct sockaddr *) (& c_add), & sin_size ); printf ("\ t \ tprocess is % ld \ n", (long) getpid (); if (-1 = nfp) {printf ("accept fail! \ R \ n "); return-1;} printf (" accept OK! \ R \ nServer start get connect from % # x: % # x \ r \ n ", ntohl (c_add.sin_addr.s_addr), ntohs (c_add.sin_port )); if (-1 = write (nfp, "hello, welcome to my server \ r \ n", 32) {printf ("write fail! \ R \ n "); return-1;} printf (" write OK! \ R \ n "); close (nfp);} close (sfp); return 0;} client # include # Include # Include # Include # Include # Include Int main () {int cfd; int recbytes; int sin_size; char buffer [1024] = {0}; struct sockaddr_in s_add, c_add; unsigned short portnum = 8888; printf ("Hello, welcome to client! \ R \ n "); cfd = socket (AF_INET, SOCK_STREAM, 0); if (-1 = cfd) {printf (" socket fail! \ R \ n "); return-1;} printf (" socket OK! \ R \ n "); bzero (& s_add, sizeof (struct sockaddr_in); s_add.sin_family = AF_INET; region = inet_addr (" 127.0.0.1 "); s_add.sin_port = htons (portnum ); printf ("s_addr =%# x, port: % # x \ r \ n", s_add.sin_addr.s_addr, s_add.sin_port); if (-1 = connect (cfd, (struct sockaddr *) (& s_add), sizeof (struct sockaddr) {printf ("connect fail! \ R \ n "); return-1;} printf (" connect OK! \ R \ n "); if (-1 = (recbytes = read (cfd, buffer, 1024) {printf (" read data fail! \ R \ n "); return-1;} printf (" read OK \ r \ nREC: \ r \ n "); buffer [recbytes] = '\ 0 '; printf ("% s \ r \ n", buffer); getchar (); close (cfd); return 0;} run the client multiple times, the displayed process numbers are different Hello, welcome to my server! Socket OK! Bind OK! Listen OK process is 3277 accept OK! Server start get connect from 0x7f000001: 0xc67d write OK! Process is 3278 accept OK! Server start get connect from 0x7f000001: 0xc681 write OK! Process is 3277 accept OK! Server start get connect from 0x7f000001: 0xc682 write OK! Process is 3278 accept OK! Server start get connect from 0x7f000001: 0xc683 write OK!
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.