码迷,mamicode.com
首页 > 编程语言 > 详细

多进程、多线程并发服务器代码

时间:2016-09-08 21:39:01      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:

1.单进程服务器:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<sys/types.h>
 4 #include<sys/socket.h>
 5 #include<string.h>
 6 #include<strings.h>
 7 #include<ctype.h>
 8 #include<arpa/inet.h>
 9 #include<unistd.h>
10 
11 #define SERV_PORT 8888
12 
13 int std_err(const char* name)
14 {
15     perror(name);
16     exit(1);
17 }
18 
19 int main(void)
20 {
21     int sfd, cfd, ret;
22     int len;
23     socklen_t clie_len;
24     char buf[BUFSIZ];
25     //创建服务器套节字
26     sfd = socket(AF_INET, SOCK_STREAM, 0);
27     if(sfd == -1)
28         std_err("socket");
29     //定义地址类型
30     struct sockaddr_in serv_addr, clie_addr;
31     serv_addr.sin_family = AF_INET;
32     serv_addr.sin_port = htons(SERV_PORT);
33     serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
34 
35     //绑定服务器的IP、端口;
36     ret = bind(sfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
37     if(ret == -1)
38         std_err("bind");
39 
40     //监听链接服务器的客户数量
41     ret = listen(sfd, 99);
42     if(ret == -1)
43         std_err("listen");
44     clie_len = sizeof(clie_addr);
45     //阻塞等待客户端发起链接请求
46     cfd = accept(sfd, (struct sockaddr*)&clie_addr, &clie_len);
47     if(cfd == -1)
48         std_err("accept");
49 
50     //传输数据
51     while(1)
52     {
53         len = read(cfd, buf, sizeof(buf));
54         printf("server: %s\n", buf);
55         int i;
56         for(i = 0; i < len; i++)
57             buf[i] = toupper(buf[i]);
58         write(cfd, buf, len);
59     }
60     close(sfd);
61     close(cfd);
62     return 0;
63 }

2.单客户端

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<sys/types.h>
 4 #include<sys/socket.h>
 5 #include<string.h>
 6 #include<strings.h>
 7 #include<ctype.h>
 8 #include<arpa/inet.h>
 9 #include<unistd.h>
10 
11 #define SERV_PORT 8888
12 
13 int std_err(const char* name)
14 {
15     perror(name);
16     exit(1);
17 }
18 
19 int main(void)
20 {
21     int cfd, ret, serv_IP;
22     char buf[BUFSIZ];
23     //创建套节字
24     cfd = socket(AF_INET, SOCK_STREAM, 0);
25     if(cfd == -1)
26         std_err("socket");
27     //定义IP , 端口
28     struct sockaddr_in clie_addr;
29     clie_addr.sin_family = AF_INET;
30     clie_addr.sin_port = htons(SERV_PORT);
31     //转换IP 字符串的地址
32     ret = inet_pton(AF_INET, "192.168.22.251", &serv_IP);
33     if(ret != 1)
34         std_err("inet_pton");
35     clie_addr.sin_addr.s_addr = serv_IP;
36     //链接服务器
37     ret = connect(cfd, (struct sockaddr*)&clie_addr, sizeof(clie_addr));
38     if(ret == -1)
39         std_err("connect");
40     // 传输数据
41     while(1)
42     {
43         int len = read(STDIN_FILENO, buf, sizeof(buf));
44         printf("cliet_len: %d\n", len);
45 
46         write(cfd, buf, len);
47         len = read(cfd, buf, sizeof(buf));
48         printf("serve_len: %d\n", len);
49         printf("%s",buf);
50         //write(STDOUT_FILENO, buf, len);     
51     }
52     //关闭套节字
53     close(cfd);
54     return 0;
55 
56 }

3.多进程服务器

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<sys/types.h>
  4 #include<sys/socket.h>
  5 #include<string.h>
  6 #include<strings.h>
  7 #include<ctype.h>
  8 #include<arpa/inet.h>
  9 #include<unistd.h>
 10 #include<errno.h>
 11 #include<sys/wait.h>
 12 #include<errno.h>
 13 
 14 #define SERV_PORT 8883
 15 
 16 int std_err(const char* name)
 17 {
 18     perror(name);
 19     exit(1);
 20 }
 21 
 22 void do_sth_child(int signum)
 23 {
 24     
 25     while(waitpid(0, NULL, WNOHANG) < 0)
 26         ;
 27     
 28 }
 29 
 30 int main(void)
 31 {
 32     int sfd, cfd, ret;
 33     int len;
 34     pid_t pid;
 35     socklen_t clie_len;
 36     char buf[BUFSIZ], clibuf[32];
 37     //创建服务器套节字
 38     sfd = socket(AF_INET, SOCK_STREAM, 0);
 39     if(sfd == -1)
 40         std_err("socket");
 41     //定义地址类型
 42     struct sockaddr_in serv_addr, clie_addr;
 43     serv_addr.sin_family = AF_INET;
 44     serv_addr.sin_port = htons(SERV_PORT);
 45     serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
 46 
 47     //绑定服务器的IP、端口;
 48     ret = bind(sfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
 49     if(ret == -1)
 50         std_err("bind");
 51 
 52     //监听链接服务器的客户数量
 53     ret = listen(sfd, 99);
 54     if(ret == -1)
 55         std_err("listen");
 56     clie_len = sizeof(clie_addr);
 57     while(1)
 58     {
 59         //阻塞等待客户端发起链接请求
 60         cfd = accept(sfd, (struct sockaddr*)&clie_addr, &clie_len);
 61         printf("client IP :%s, port: %d\n",
 62                 inet_ntop(sfd, &clie_addr.sin_addr.s_addr, clibuf, sizeof(clibuf)),
 63                ntohs(clie_addr.sin_port) );
 64         if(cfd == -1)
 65             std_err("accept");
 66         pid = fork();
 67         if(pid < 0)
 68             std_err("fork:");
 69         else if(pid == 0)
 70         {
 71             close(sfd);
 72             break;
 73         }
 74         else        //住进程实现逻辑;1.回收子进程,2,关闭不必要的文件描述父 3,继续等待客户端链接,如果有,则继续创建子进程
 75         {
 76             close(cfd);
 77             signal(SIGCHLD, do_sth_child);
 78         }
 79     }
 80     if(pid == 0)
 81     {
 82 agian:
 83         //子进程传输数据
 84         while(1)
 85         {
 86             len = read(cfd, buf, sizeof(buf));
 87             if(len == 0)    //即客户端关闭通信,
 88             {
 89                 close(cfd);
 90                 exit(1);
 91             }
 92             else if( len == -1)
 93             {
 94                 if(errno == EINTR)
 95                 {
 96                     goto agian;
 97                 }
 98                 else
 99                 {
100                     std_err("read:");
101                 }
102             }
103             else
104             {
105                 int i;
106                 for(i = 0; i < len; i++)
107                     buf[i] = toupper(buf[i]);
108                 write(cfd, buf, len);
109                 write(STDOUT_FILENO, buf, len);
110             }
111         }
112     }
113 
114     return 0;
115 }

4.多个客户端(一段代码产生多个客户端)

  1.当时编写这段代码的作用是撑爆老师的服务器

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<sys/types.h>
  4 #include<sys/socket.h>
  5 #include<string.h>
  6 #include<strings.h>
  7 #include<ctype.h>
  8 #include<arpa/inet.h>
  9 #include<unistd.h>
 10 #include<sys/wait.h>
 11 #include<signal.h>
 12 #include<errno.h>
 13 #include<pthread.h>
 14 #include <pthread.h>
 15 
 16 
 17 #define SERV_PORT 8883
 18 #define SERV_IP "127.0.0.1"
 19 int std_err(const char* name)
 20 {
 21     perror(name);
 22     exit(1);
 23 }
 24 void do_sth_child(int signum)
 25 {
 26     while(waitpid(0, NULL, WNOHANG) > 0);
 27 }
 28 
 29 int main(void)
 30 {
 31     int cfd, ret;
 32     char buf[BUFSIZ];
 33     pid_t pid;
 34     //pthread_mutexattr_t mattr;
 35     //pthread_mutexattr_init(&mattr);
 36     //pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
 37     //pthread_mutex_t mutex;
 38     //pthread_mutex_init(&mutex, &mattr);
 39 
 40     int i;
 41     for(i = 0; i < 6; i++){
 42         pid = fork();
 43         if(pid == 0)
 44             break;
 45         else if(pid < 0)
 46             std_err("fork");
 47         else
 48         {
 49             // close(cfd);
 50             signal(SIGCHLD, do_sth_child);
 51         }
 52     }
 53 
 54     //子进程逻辑
 55     if(pid == 0)
 56     {
 57         //创建套节字
 58         cfd = socket(AF_INET, SOCK_STREAM, 0);
 59         if(cfd == -1)
 60             std_err("socket");
 61         //定义IP , 端口
 62         struct sockaddr_in clie_addr;
 63         clie_addr.sin_family = AF_INET;
 64         clie_addr.sin_port = htons(SERV_PORT);
 65         //转换IP 字符串的地址
 66         ret = inet_pton(AF_INET, SERV_IP, &clie_addr.sin_addr.s_addr);
 67         if(ret != 1)
 68             std_err("inet_pton");
 69         //pthread_mutex_lock(&mutex);
 70         //链接服务器
 71         ret = connect(cfd, (struct sockaddr*)&clie_addr, sizeof(clie_addr));
 72         if(ret == -1)
 73             std_err("connect");
 74         // 传输数据
 75         // while(1)
 76         {
 77             int len;
 78 again:
 79             //char bufstr[10] = "sdasdasd";
 80             len = read(STDIN_FILENO, buf, sizeof(buf));
 81             if(len < 0)
 82             {
 83                 if(errno == EAGAIN || errno == EINTR)
 84                     goto again;
 85                 else
 86                     std_err("read");
 87             }
 88             write(cfd, buf, len);
 89             //write(cfd, bufstr, sizeof(bufstr));
 90             //len = read(cfd, buf, sizeof(buf));
 91             //printf("serve_len: %d\n", len);
 92             //printf("%s",buf);
 93             write(STDOUT_FILENO, buf, len);     
 94         }
 95         //pthread_mutex_unlock(&mutex);
 96     }
 97     while(1);
 98     //关闭套节字
 99     close(cfd);
100     return 0;
101 
102 }

 

多进程、多线程并发服务器代码

标签:

原文地址:http://www.cnblogs.com/yyx1-1/p/5854408.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!