C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/epoll.h>
#include <errno.h>

static int
make_socket_non_blocking (int sfd)
{
    int flags, s;

flags = fcntl (sfd, F_GETFL, );
    )
    {
        perror ("fcntl");
        ;
    }

flags |= O_NONBLOCK;
    s = fcntl (sfd, F_SETFL, flags);
    )
    {
        perror ("fcntl");
        ;
    }

;
}

static int
create_and_bind (char *port)
{
    struct addrinfo hints;
    struct addrinfo *result, *rp;
    int s, sfd;

memset (&hints, , sizeof (struct addrinfo));
    hints.ai_family = AF_UNSPEC;     /* Return IPv4 and IPv6 choices */
    hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */
    hints.ai_flags = AI_PASSIVE;     /* All interfaces */

s = getaddrinfo (NULL, port, &hints, &result);
    )
    {
        fprintf (stderr, "getaddrinfo: %s\n", gai_strerror (s));
        ;
    }

for (rp = result; rp != NULL; rp = rp->ai_next)
    {
        sfd = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol);
        )
        {
            continue;
        }

s = bind (sfd, rp->ai_addr, rp->ai_addrlen);
        )
        {
            /* We managed to bind successfully! */
            break;
        }

close (sfd);
    }

if (rp == NULL)
    {
        fprintf (stderr, "Could not bind\n");
        ;
    }

freeaddrinfo (result);

return sfd;
}

int
main (int argc, char *argv[])
{
    int sfd, s;
    int efd;
    struct epoll_event event;
    struct epoll_event *events;

)
    {
        fprintf (stderr, ]);
        exit (EXIT_FAILURE);
    }

sfd = create_and_bind (argv[]);
    )
    {
        abort ();
    }

s = make_socket_non_blocking (sfd);
    )
    {
        abort ();
    }

s = listen (sfd, SOMAXCONN);
    )
    {
        perror ("listen");
        abort ();
    }

efd = epoll_create1 ();
    )
    {
        perror ("epoll_create");
        abort ();
    }

event.data.fd = sfd;
    event.events = EPOLLIN | EPOLLET;
    s = epoll_ctl (efd, EPOLL_CTL_ADD, sfd, &event);
    )
    {
        perror ("epoll_ctl");
        abort ();
    }

/* Buffer where events are returned */
    events = calloc (MAXEVENTS, sizeof event);

/* The event loop */
    )
    {
        int n, i;

n = epoll_wait (efd, events, MAXEVENTS, -);
        ; i < n; i++)
        {
            if ((events[i].events & EPOLLERR) ||
                    (events[i].events & EPOLLHUP) ||
                    (!(events[i].events & EPOLLIN)))
            {
                /* An error has occured on this fd, or the socket is not
                   ready for reading (why were we notified then?) */
                fprintf (stderr, "epoll error\n");
                close (events[i].data.fd);
                continue;
            }

else if (sfd == events[i].data.fd)
            {
                /* We have a notification on the listening socket, which
                   means one or more incoming connections. */
                )
                {
                    struct sockaddr in_addr;
                    socklen_t in_len;
                    int infd;
                    char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];

in_len = sizeof in_addr;
                    infd = accept (sfd, &in_addr, &in_len);
                    )
                    {
                        if ((errno == EAGAIN) ||
                                (errno == EWOULDBLOCK))
                        {
                            /* We have processed all incoming
                               connections. */
                            break;
                        }
                        else
                        {
                            perror ("accept");
                            break;
                        }
                    }

s = getnameinfo (&in_addr, in_len,
                                     hbuf, sizeof hbuf,
                                     sbuf, sizeof sbuf,
                                     NI_NUMERICHOST | NI_NUMERICSERV);
                    )
                    {
                        printf("Accepted connection on descriptor %d "
                               "(host=%s, port=%s)\n", infd, hbuf, sbuf);
                    }

/* Make the incoming socket non-blocking and add it to the
                       list of fds to monitor. */
                    s = make_socket_non_blocking (infd);
                    )
                    {
                        abort ();
                    }

event.data.fd = infd;
                    event.events = EPOLLIN | EPOLLET;
                    s = epoll_ctl (efd, EPOLL_CTL_ADD, infd, &event);
                    )
                    {
                        perror ("epoll_ctl");
                        abort ();
                    }
                }
                continue;
            }
            else
            {
                /* We have data on the fd waiting to be read. Read and
                   display it. We must read whatever data is available
                   completely, as we are running in edge-triggered mode
                   and won't get a notification again for the same
                   data. */
                ;

)
                {
                    ssize_t count;
                    ];

count = read (events[i].data.fd, buf, sizeof buf);
                    )
                    {
                        /* If errno == EAGAIN, that means we have read all
                           data. So go back to the main loop. */
                        if (errno != EAGAIN)
                        {
                            perror ("read");
                            done = ;
                        }
                        break;
                    }
                    )
                    {
                        /* End of file. The remote has closed the
                           connection. */
                        done = ;
                        break;
                    }

/* Write the buffer to standard output */
                    s = write (, buf, count);
                    )
                    {
                        perror ("write");
                        abort ();
                    }
                }

if (done)
                {
                    printf ("Closed connection on descriptor %d\n",
                            events[i].data.fd);

/* Closing the descriptor will make epoll remove it
                       from the set of descriptors which are monitored. */
                    close (events[i].data.fd);
                }
            }
        }
    }

free (events);

close (sfd);

return EXIT_SUCCESS;
}

具体函数介绍请参考:https://banu.com/blog/2/how-to-use-epoll-a-complete-example-in-c/

运行结果:

服务器端:

客户端:

epoll测试实例的更多相关文章

  1. JMeter学习-026-JMeter 分布式(远程)参数化测试实例

    以前文所述对文章详情的HTTP请求进行性能测试为例.日常实际场景中,不可能所有的人都在同时访问一篇文章,而是多人访问不同的文章,因而需要对文章编号进行参数化,以更好的模拟日常的性能测试场景.同时,因文 ...

  2. webservice测试实例

    webservice测试实例(LR8.1) 接口声明:这个接口是sina的短信服务接口,我只是用来做脚本学习使用,不会对其产生压力:希望读者也只是用来进行录制学习,而不是产生压力. 接口文档:http ...

  3. [原]在Fedora中编译Libevent测试实例

    在我的昨天的博文<[原]我在Windows环境下的首个Libevent测试实例>中介绍了在Windows环境下如何编译一个echo server例子.今天我又试了一下在Linux环境中编译 ...

  4. C++动态链接库测试实例

    前话 上一章节我导出了一个动态链接库 要使用该链接库,我们还需要该链接库对外公开的函数,即头文件 下面开始实例 测试实例 第一步--将动态链接库的dll.lib.和头文件导入项目中 文件目录如下: 项 ...

  5. Linux下简易蜂鸣器驱动代码及测试实例

    驱动代码: #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> ...

  6. 微服务架构 - 离线部署k8s平台并部署测试实例

    一般在公司部署或者真实环境部署k8s平台,很有可能是内网环境,也即意味着是无法连接互联网的环境,这时就需要离线部署k8s平台.在此整理离线部署k8s的步骤,分享给大家,有什么不足之处,欢迎指正. 1. ...

  7. WinForm中 Asp.Net Signalr消息推送测试实例

    p{ text-align:center; } blockquote > p > span{ text-align:center; font-size: 18px; color: #ff0 ...

  8. Confluence 6 从生产环境中恢复一个测试实例

    请参考 Restoring a Test Instance from Production 页面中的内容获得更多完整的说明. 很多 Confluence 的管理员将会使用生产实例运行完整数据和服务的 ...

  9. Rabbit简单测试实例

    Rabbit简单测试实例 安装环境: Yum -y install python-pip Pip install pika 生产者 1 2 3 4 5 6 7 8 9 10 11 import pik ...

随机推荐

  1. bzoj 1539: [POI2005]Dwu-Double-row

    假设一列交换表示为1,不换表示为0. 身高相同的两个人相当于给其中两列了一个限制条件,要么是两个必须相等,要么一个为零一个为一. 有了关系后我们就可以把每列当成一个点建边,边权为0表示必须相同,1为必 ...

  2. 使用JS在textarea在光标处插入内容

    // 在光标处插入字符串 // myField 文本框对象 // myValue 要插入的值 function insertAtCursor(myField, myValue) { //IE supp ...

  3. 团体程序设计天梯赛 L3-012. 水果忍者

    /*对于一条满足条件的直线,向下移,直到触碰一条线段的下端点,仍然经过其它线段,该直线仍然满足条件 即以一条线段的下(上)端点作为直线上的一点,求为了经过一条线段的最小.最大斜率值(mink,maxk ...

  4. 非极大值抑制Non-Maximum Suppression(NMS)

    非极大值抑制(Non-Maximum Suppression,NMS)   概述 非极大值抑制(Non-Maximum Suppression,NMS),顾名思义就是抑制不是极大值的元素,可以理解为局 ...

  5. 1130 N的阶乘的长度 V2(斯特林近似)

    1130 N的阶乘的长度 V2(斯特林近似) 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 输入N求N的阶乘的10进制表示的长度.例如6! = 720, ...

  6. HDU 1525 类Bash博弈

    给两数a,b,大的数b = b - a*k,a*k为不大于b的数,重复过程,直到一个数为0时,此时当前操作人胜. 可以发现如果每次b=b%a,那么GCD的步数决定了先手后手谁胜,而每次GCD的一步过程 ...

  7. php设计模式-工厂设计模式

    概念: 工厂设计模式提供获取某个对象的新实例的一个接口,同时使调用代码避免确定实际实例化基类步骤. 很多高级模式都是依赖于工厂模式.

  8. windows环境下git的环境变量配置

    1.从官网下载git这个软件.msi格式,然后安装. 2.找到你的git的安装目录,并记录下来 3.配置环境变量:在path里加入——  ;你的git的安装目录\bin;你的git的安装目录\libe ...

  9. JS模块规范

    ES6标准发布后,module成为标准,标准的使用是以export指令导出接口,以import引入模块,但是在我们一贯的node模块中,我们采用的是CommonJS规范,使用require引入模块,使 ...

  10. GRUB (简体中文)

    原文链接:https://wiki.archlinux.org/index.php/GRUB_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87) 前言 引导程序是计算机启动时 ...