linux系統父子進程共用連接埠

來源:互聯網
上載者:User

linux系統父子進程共用連接埠 nginx 就是使用了父子進程 共用連接埠,所以nginx快速這個也算一個原因  #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <stdio.h> #include <sys/socket.h> #include <netinet/in.h> #include <string.h>  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; s_add.sin_addr.s_addr=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\t\t\t\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; }  用戶端 #include <stdlib.h> #include <sys/types.h> #include <stdio.h> #include <sys/socket.h> #include <netinet/in.h> #include <string.h>  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; s_add.sin_addr.s_addr= 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; }  分別多次運行用戶端,可以發現顯示的進程號是不同的  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!  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.