78.pipe多管道云端,客户端通信

压力测试截图:

 

云端

  • 定义管道缓存区大小,最多连接数量(线程个数),当前线程个数,管道名字
    1 //缓冲区大小
    2 #define SIZE 4096
    3 //最多连接数量
    4 #define MAX_CONNECT 128
    5 //一开始有10个线程存在
    6 int  startthreadnum = 10;
    7 //管道名字
    8 char  pipename[128] = "\\\\.\\Pipe\\cloudpipe";

     

  • 创建结构体,存储线程,管道和事件的信息
    1 //创建结构体,存储线程,管道和事件的信息
    2 typedef struct info
    3 {
    4     HANDLE hthread;
    5     HANDLE hpipe;//管道信息
    6     HANDLE hevent;//事件用于初始化一个结构体用于连接管道,并存储连接的信息
    7 }PIPE_ST;

     

  • 创建结构体
    1 //128个结构体
    2 PIPE_ST  pipeinst[MAX_CONNECT];

     

  • 初始化结构体,并开启线程
     1 //创建管道,事件,并启动线程
     2 void start()
     3 {
     4     for (int i = 0; i <startthreadnum; i++)
     5     {
     6         //创建管道,如果同名则操作同一个管道
     7         pipeinst[i].hpipe = CreateNamedPipeA(
     8             pipename,//管道名称
     9             PIPE_ACCESS_DUPLEX|FILE_FLAG_OVERLAPPED,//管道读写属性
    10             PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,//消息模式,读模式,等待模式阻塞
    11             10,//最多使用本管道的实例个数
    12             0,//输出缓冲区大小
    13             0,//输入缓冲区大小
    14             1000,//超时,无限等待
    15             NULL);
    16         if (pipeinst[i].hpipe == INVALID_HANDLE_VALUE)
    17         {
    18             printf("\n%d失败",i);
    19             return;
    20         }
    21         //创建事件
    22         pipeinst[i].hevent = CreateEventA(NULL, FALSE, FALSE, FALSE);//创建事件
    23         //创建线程
    24         pipeinst[i].hthread=CreateThread(NULL,0,severThread,&pipeinst[i],0,NULL);
    25     }
    26     printf("sever start");
    27 
    28 }

     

  • 线程函数
     1 //服务器线程
     2 DWORD WINAPI severThread(void *lp)
     3 {
     4     //存储读取的数量
     5     DWORD nread = 0;
     6     //存储写入的数量
     7     DWORD nwrite = 0;
     8     //检测IO是否完成
     9     DWORD dwbyte = 0;
    10     char szbuf[SIZE] = { 0 };
    11 
    12     //获取当前结构体
    13     PIPE_ST curpipe = *(PIPE_ST*)lp;
    14     //用事件初始化一个结构体用于连接管道,存储连接的信息
    15     OVERLAPPED overlap = { 0, 0, 0, 0, curpipe.hevent };
    16 
    17     while (1)
    18     {
    19         //数据清零
    20         memset(szbuf, 0, sizeof(szbuf));
    21         //链接上管道,并把信息写入overlap
    22         ConnectNamedPipe(curpipe.hpipe, &overlap);
    23         //等待
    24         WaitForSingleObject(curpipe.hevent, INFINITE);
    25         //检测IO,如果完成就跳出
    26         if (!GetOverlappedResult(curpipe.hpipe,&overlap,&dwbyte,TRUE))
    27         {
    28             break;
    29         }
    30         //读取管道中的数据到szbuf中
    31         if (!ReadFile(curpipe.hpipe,szbuf,SIZE,&nread,NULL))
    32         {
    33             puts("read fail");
    34             break;
    35         }
    36         //从读取的数据中获取数据
    37         int a, b;
    38         sscanf(szbuf, "%d %d", &a, &b);
    39         //缓存区清零
    40         memset(szbuf, 0, sizeof(szbuf));
    41         //计算结果
    42         sprintf(szbuf, "%d", a + b);
    43         //把结果写入管道
    44         WriteFile(curpipe.hpipe, szbuf, strlen(szbuf), &nwrite, NULL);
    45         //断开连接
    46         DisconnectNamedPipe(curpipe.hpipe);
    47     }
    48     return 0;
    49 }

     

客户端

  • 定义管道缓存区大小,管道名字,以及管道连接的句柄
    1 #define SIZE 4096
    2 char  pipename[128] = "\\\\.\\Pipe\\cloudpipe";
    3 HANDLE m_pipe = NULL;

     

  • 生成随机数用于给服务器进行计算
     1 int a;
     2 int b;
     3 void run()
     4 {
     5     time_t ts;
     6     unsigned int num = time(&ts);
     7     srand(num);
     8     a = rand() % 1000;
     9     b= rand() % 1000;
    10 }

     

  • 打开管道,并向管道中写入数据,再读取计算后的结果
     1 m_pipe = CreateFileA(pipename, //名称
     2         GENERIC_WRITE | GENERIC_READ,//读写
     3         0,//共享属性,1独有
     4         NULL,//默认安全属性
     5         OPEN_EXISTING,//打开已经存在的
     6         FILE_ATTRIBUTE_NORMAL,
     7         NULL);
     8 
     9     if (m_pipe==INVALID_HANDLE_VALUE)
    10     {
    11         printf("失败");
    12         return;
    13     }
    14     //存储读取写入了多少个
    15     int nwrite;
    16     int nread;
    17     //生成随机数
    18     run();
    19     //存储写入的数据
    20     char winfo[1024] = { 0 };
    21     //格式到winfo中
    22     sprintf(winfo, "%d %d", a, b);
    23     //写入管道
    24     WriteFile(m_pipe, winfo, strlen(winfo), &nwrite, NULL);
    25     //清零
    26     memset(winfo, 0, sizeof(winfo));
    27     //读取管道计算后的数据到winfo中
    28     ReadFile(m_pipe, winfo, 1024, &nread, NULL);
    29     //获取服务器计算的结果
    30     int  res;
    31     sscanf(winfo, "%d", &res);
    32     printf("\n%d+%d=%d", a, b, res);

     

服务器端完整代码:

  1 #define  _CRT_SECURE_NO_WARNINGS
  2 #include<stdio.h>
  3 #include<time.h>
  4 #include<stdlib.h>
  5 #include<Windows.h>
  6 
  7 //缓冲区大小
  8 #define SIZE 4096
  9 //最多连接数量
 10 #define MAX_CONNECT 128
 11 //一开始有10个线程存在
 12 int  startthreadnum = 10;
 13 //管道名字
 14 char  pipename[128] = "\\\\.\\Pipe\\cloudpipe";
 15 
 16 //创建结构体,存储线程,管道和事件的信息
 17 typedef struct info
 18 {
 19     HANDLE hthread;
 20     HANDLE hpipe;//管道信息
 21     HANDLE hevent;//事件用于初始化一个结构体用于连接管道,并存储连接的信息
 22 }PIPE_ST;
 23 
 24 //128个结构体
 25 PIPE_ST  pipeinst[MAX_CONNECT];
 26 
 27 //服务器线程
 28 DWORD WINAPI severThread(void *lp)
 29 {
 30     //存储读取的数量
 31     DWORD nread = 0;
 32     //存储写入的数
 33     DWORD nwrite = 0;
 34     //检测IO是否完成
 35     DWORD dwbyte = 0;
 36     char szbuf[SIZE] = { 0 };
 37 
 38     //获取当前结构体
 39     PIPE_ST curpipe = *(PIPE_ST*)lp;
 40     //用事件初始化一个结构体用于连接管道,存储连接的信息
 41     OVERLAPPED overlap = { 0, 0, 0, 0, curpipe.hevent };
 42 
 43     while (1)
 44     {
 45         //数据清零
 46         memset(szbuf, 0, sizeof(szbuf));
 47         //链接上管道,并把信息写入overlap
 48         ConnectNamedPipe(curpipe.hpipe, &overlap);
 49         //等待
 50         WaitForSingleObject(curpipe.hevent, INFINITE);
 51         //检测IO,如果完成就跳出
 52         if (!GetOverlappedResult(curpipe.hpipe,&overlap,&dwbyte,TRUE))
 53         {
 54             break;
 55         }
 56         //读取管道中的数据到szbuf中
 57         if (!ReadFile(curpipe.hpipe,szbuf,SIZE,&nread,NULL))
 58         {
 59             puts("read fail");
 60             break;
 61         }
 62         //从读取的数据中获取数据
 63         int a, b;
 64         sscanf(szbuf, "%d %d", &a, &b);
 65         //缓存区清零
 66         memset(szbuf, 0, sizeof(szbuf));
 67         //计算结果
 68         sprintf(szbuf, "%d", a + b);
 69         //把结果写入管道
 70         WriteFile(curpipe.hpipe, szbuf, strlen(szbuf), &nwrite, NULL);
 71         //断开连接
 72         DisconnectNamedPipe(curpipe.hpipe);
 73     }
 74     return 0;
 75 }
 76 
 77 //创建管道,事件,并启动线程
 78 void start()
 79 {
 80     for (int i = 0; i <startthreadnum; i++)
 81     {
 82         //创建管道
 83         pipeinst[i].hpipe = CreateNamedPipeA(
 84             pipename,//管道名称
 85             PIPE_ACCESS_DUPLEX|FILE_FLAG_OVERLAPPED,//管道读写属性
 86             PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,//消息模式,读模式,等待模式阻塞
 87             10,//最多使用本管道的实例个数
 88             0,//输出缓冲区大小
 89             0,//输入缓冲区大小
 90             1000,//超时,无限等待
 91             NULL);
 92         if (pipeinst[i].hpipe == INVALID_HANDLE_VALUE)
 93         {
 94             printf("\n%d失败",i);
 95             return;
 96         }
 97         //创建事件
 98         pipeinst[i].hevent = CreateEventA(NULL, FALSE, FALSE, FALSE);//创建事件
 99         //创建线程
100         pipeinst[i].hthread=CreateThread(NULL,0,severThread,&pipeinst[i],0,NULL);
101     }
102     printf("sever start");
103 
104 }
105 
106 void main()
107 {
108     start();
109     system("pause");
110 }

 

客户端完整代码:

 1 #define  _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<time.h>
 4 #include<stdlib.h>
 5 #include<Windows.h>
 6 
 7 #define SIZE 4096
 8 char  pipename[128] = "\\\\.\\Pipe\\cloudpipe";
 9 HANDLE m_pipe = NULL;
10 
11 
12 int a;
13 int b;
14 void run()
15 {
16     time_t ts;
17     unsigned int num = time(&ts);
18     srand(num);
19     a = rand() % 1000;
20     b= rand() % 1000;
21 }
22 
23 
24 
25 void main()
26 {
27     m_pipe = CreateFileA(pipename, //名称
28         GENERIC_WRITE | GENERIC_READ,//读写
29         0,//共享属性,1独有
30         NULL,//默认安全属性
31         OPEN_EXISTING,//打开已经存在的
32         FILE_ATTRIBUTE_NORMAL,
33         NULL);
34 
35     if (m_pipe==INVALID_HANDLE_VALUE)
36     {
37         printf("失败");
38         return;
39     }
40     //存储读取写入了多少个
41     int nwrite;
42     int nread;
43     //生成随机数
44     run();
45     //存储写入的数据
46     char winfo[1024] = { 0 };
47     //格式到winfo中
48     sprintf(winfo, "%d %d", a, b);
49     //写入管道
50     WriteFile(m_pipe, winfo, strlen(winfo), &nwrite, NULL);
51     //清零
52     memset(winfo, 0, sizeof(winfo));
53     //读取管道计算后的数据到winfo中
54     ReadFile(m_pipe, winfo, 1024, &nread, NULL);
55     //获取服务器计算的结果
56     int  res;
57     sscanf(winfo, "%d", &res);
58     printf("\n%d+%d=%d", a, b, res);
59     system("pause");
60 }

 压力测试代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <Windows.h>
 4 
 5 void main()
 6 {
 7 
 8 
 9     while (1)
10     {
11         for (int i = 0; i < 10;i++)
12         {
13             ShellExecuteA(NULL, "open", "E:\\工具\\20150523\\pipe\\Debug\\客户端.exe", NULL, NULL, 1);
14             Sleep(100);
15         }
16 
17 
18         
19     }
20 
21 
22 
23 }

 

posted @ 2018-02-20 15:43  喵小喵~  阅读(373)  评论(0编辑  收藏  举报