TCP并发服务器多进程

avatar
作者
猴君
阅读量:0

 service

#include<stdio.h> #include <sys/types.h> #include <sys/wait.h> #include <signal.h>  #include<unistd.h> #include<string.h> #include<strings.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include<stdlib.h> #define PORT 5001 #define STR_QUIT "quit" void child_data_handle(int signum) {         if(SIGCHLD == signum)         {                 waitpid(-1,NULL,WNOHANG);         }  } void* client_data_handle(void* arg);  int main() {          int fd = -1;         int newfd = -1;         pid_t pid;          signal(SIGCHLD,child_data_handle);          fd = socket(AF_INET,SOCK_STREAM,0);         if(fd<0)         {                 perror("socket");                 exit(1);         }          struct sockaddr_in sin;         bzero(&sin,sizeof(sin));         sin.sin_family = AF_INET;         sin.sin_port = htons(PORT);         sin.sin_addr.s_addr = INADDR_ANY;          if(bind(fd,(struct sockaddr *)&sin,sizeof(sin))<0)         {                 perror("bind");                 exit(1);          }          if(listen(fd,5)<0)         {                 perror("listen");                 exit(1);         }         pid_t pid;          int newfd = -1;         struct sockaddr_in cin;         bzero(&sin,sizeof(cin));         socklen_t sockaddr_len = sizeof(cin);          while(1)         {                 newfd = accept(fd,(struct sockaddr *)&cin.sin_addr,&sockaddr_len);                         if(newfd<0)                         {                                 perror("accept");                                 exit(1);                         }                  pid = fork();                  if(pid<0)                 {                         perror("fork");                         exit(1);                 }                 if(pid == 0)                 {                          char ipv4[16];                          if(!inet_ntop(newfd,(void *)&cin.sin_addr,ipv4,sizeof(cin)))                         {                                 perror("inet_ntop");                                 exit(1);                         }                         printf("client (%s,%d) is connect\n",ipv4,ntohs(cin.sin_port));                           client_data_handle(&newfd);                         close(fd);                 }                  if(pid>0)                  {                         close(newfd);                 }          }         close(fd);         return 0; }  void* client_data_handle (void* arg)  {         char buf[1024];         int newfd = *(int*)arg;         int ret =-1;         printf("child handle process:newfd = %d\n",newfd);         while(1)         {                  do{                         ret = read(newfd,&buf,1023);                   }while(ret<1);                  if(!ret)                 {                         break;                 }                 if(ret<0)                 {                         exit(1);                 }                  printf("receive data is %s\n",buf);                 if(!strncasecmp(buf,STR_QUIT,strlen(STR_QUIT)))                 {                         printf("client is exiting!");                         break;                 }         }  }  

client

#include<stdio.h> #include <sys/socket.h> #include <netinet/in.h> #include <netinet/ip.h> /* superset of previous */ #include<stdlib.h> #include <strings.h> #include<unistd.h>  #include <string.h>  #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h>  #include <sys/types.h>          /* See NOTES */ #include <sys/socket.h> //./client 192.168.182.144 5001     #define SERV_PORT 5001 #define SERV_IP_ADDR "192.168.157.34" #define BUFSIZE 1024 #define QUIT_STR "QUIT"   int main(int argc,char **argv) {         int fd = -1;         if(argc!=3)         {         exit(1);         }         int port = -1;         port = atoi(argv[2]);         fd = socket(AF_INET,SOCK_STREAM,0);         if(fd<0)         {                  perror("socket");                 exit(1);         }          struct sockaddr_in sin;       bzero(&sin,sizeof(sin));         sin.sin_family = AF_INET;         sin.sin_port = htons(port);         sin.sin_addr.s_addr = inet_addr(argv[1]);          if(connect(fd,(struct sockaddr*)&sin,sizeof(sin))<0)         {                 perror("connect");                 exit(1);         }         char buf[BUFSIZE];         while(1)         {                 bzero(buf,BUFSIZE);                 if(fgets(buf,BUFSIZE-1,stdin)==NULL)                 {                         continue;                 }                write(fd,buf,strlen(buf));                 if(!strncasecmp(buf,QUIT_STR,strlen(QUIT_STR)))                 {                          break;                 }           }         return 0;   } 

结果

 

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!