linux TCP/IP網路編程(多進程)

來源:互聯網
上載者:User

 

多進程網路編程案例如下

#include <stdio.h>#include <errno.h>#include <netdb.h>#include <sys/socket.h>// may be use by getpid() and getppid()#include <unistd.h>int main(int argc, char *argv[]) {   int sockfd,new_fd, mypid;   struct sockaddr_in server_addr;   struct sockaddr_in client_addr;   int sin_size,portnumber;   char hello[]="Hello! Are You Fine?";   char buf[1024];   int flag;   if(argc!=2)   {     fprintf(stderr,"Usage:%s portnumber\a\n",argv[0]);     exit(1);   }   if((portnumber=atoi(argv[1]))<0)   {     fprintf(stderr,"Usage:%s portnumber\a\n",argv[0]);     exit(1);   }   /* 伺服器端開始建立socket描述符 */   if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)   {     fprintf(stderr,"Socket error:%s\n\a",strerror(errno));     exit(1);   }   /* 伺服器端填充 sockaddr結構 */   bzero(&server_addr,sizeof(struct sockaddr_in));   server_addr.sin_family=AF_INET;   server_addr.sin_addr.s_addr=htonl(INADDR_ANY);   server_addr.sin_port=htons(portnumber);   /* 捆綁sockfd描述符 */   if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)   {     fprintf(stderr,"Bind error:%s\n\a",strerror(errno));     exit(1);   }   /* 監聽sockfd描述符 */   if(listen(sockfd,5)==-1)   {     fprintf(stderr,"Listen error:%s\n\a",strerror(errno));     exit(1);   }    // printf my PID num  printf("Starting Server at port: %d Success. My PID is: %d\nAccepting Request...\n", portnumber, getpid());  while(1)   {     /* 伺服器阻塞,直到客戶程式建立串連 */     sin_size=sizeof(struct sockaddr_in);     if((new_fd=accept(sockfd,(struct sockaddr *)(&client_addr),&sin_size))==-1)     {       fprintf(stderr,"Accept error:%s\n\a",strerror(errno));       exit(1);     }         // 產生一個子進程來完成和用戶端的會話,父進程繼續監聽    if (!fork())    {      // print my PID and my parent PID      mypid = getpid();      printf("Accepting new connection...\nNow I will server for you. My PID is: %d\nMy parent PID is: %d\n", mypid, getppid());      // 子進程,關閉父進程監聽通訊端      close(sockfd);      fprintf(stderr,"Server get connection from %s\n",       inet_ntoa(client_addr.sin_addr));       if(write(new_fd,hello,strlen(hello))==-1)       {         fprintf(stderr,"Write Error:%s\n",strerror(errno));         exit(1);       }       while(1) //get data from client      {        if((flag=read(new_fd,buf,1024))<0)        {            printf("Reading data error");          break;        }        if(flag==0)        {          printf("[PID:%d] Ending current connection\n",mypid);                    break;        }         else        {          printf("[PID:%d] -->%s\n",mypid,buf);          if(strstr(buf,"exit"))          {    printf("I get 'exit' string, so close client connection\n");    break;          }         }             }  //end while (get data from client)         // 串連丟失,結束進程 (客戶主動斷開,網路問題自動斷開)      printf("Connection lose. End child program, PID: %d\n", getpid());      close(new_fd);      exit(0);    } // if fork        // 父進程關閉新通訊端,繼續監聽請求    close(new_fd);    }  //while    close(sockfd);   exit(0); } 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.