朴素、Select、Poll和Epoll网络编程模型实现和分析——Poll模型
来源:互联网 发布:versions 1.7 for mac 编辑:程序博客网 时间:2024/05/29 04:34
在《朴素、Select、Poll和Epoll网络编程模型实现和分析——Select模型》中,我们分析了它只能支持1024个连接同时处理的原因。但是在有些需要同时处理更多连接的情况下,1024个连接往往是不够的,也就是不能够高并发。那么这个时候我们就可以采用本文介绍的Poll模型。(转载请指明出于breaksoftware的csdn博客)
在使用Poll模型之前,我们需要定义一个保存连接信息的数组
- struct pollfd fds[FDS_COUNT];
之后创建异步监听socket、绑定端口和监听端口等行为和《朴素、Select、Poll和Epoll网络编程模型实现和分析——Select模型》一文中一模一样,本文就不列出代码了。我们把创建的socket信息赋值给fds数组的第一个元素,并且设定我们需要关注的事件POLLIN——可读。
- int timeout;
- int cur_fds_count;
- int rc;
- int index;
- int expect_events;
- int error_events;
- ……// 创建socket
- memset(fds, 0, sizeof(fds));
- error_events = POLLERR | POLLNVAL;
- expect_events = POLLIN;
- fds[0].fd = listen_sock;
- fds[0].events = expect_events;
- cur_fds_count = 1;
然后我们就要在一个死循环中,不停的调用poll函数,监控我们关心的文件描述符是否发生了状态改变
- timeout = (500);
- while (1) {
- rc = poll(fds, cur_fds_count, timeout);
- if (rc < 0) {
- perror("poll error\n");
- exit(EXIT_FAILURE);
- };
- if (rc == 0) {
- //perror("poll timeout\n");
- };
- int cur_fds_count_temp = cur_fds_count;
- for (index = 0; index < cur_fds_count_temp; ++index) {
- struct pollfd {
- int fd;
- short events;
- short revents;
- };
- if (fds[index].revents == 0) {
- continue;
- }
- if (fds[index].revents & error_events) {
- perror("revents error");
- if (fds[index].fd == listen_sock) {
- perror("listen sock error");
- exit(EXIT_FAILURE);
- }
- else {
- close(fds[index].fd);
- cur_fds_count--;
- if (index < cur_fds_count) {
- memcpy(&fds[index], &fds[cur_fds_count], sizeof(fds[cur_fds_count]));
- memset(&fds[cur_fds_count], 0, sizeof(fds[cur_fds_count]));
- index--;
- }
- cur_fds_count_temp--;
- continue;
- }
- if (!(fds[index].revents & fds[index].events)) {
- continue;
- }
- if (fds[index].fd == listen_sock) {
- int new_sock;
- new_sock = accept(listen_sock, NULL, NULL);
- if (new_sock < 0) {
- //perror("accept error");
- if (errno != EWOULDBLOCK) {
- continue;
- }
- exit(EXIT_FAILURE);
- }
- else {
- request_add(1);
- //set_block_filedes_timeout(new_sock);
- if (cur_fds_count + 1 < sizeof(fds)) {
- fds[cur_fds_count].fd = new_sock;
- fds[cur_fds_count].events = expect_events;
- cur_fds_count++;
- }
- }
- }
- else {
- if (0 == server_read(fds[index].fd)) {
- server_write(fds[index].fd);
- }
- close(fds[index].fd);
- cur_fds_count--;
- if (index < cur_fds_count) {
- memcpy(&fds[index], &fds[cur_fds_count], sizeof(fds[cur_fds_count]));
- memset(&fds[cur_fds_count], 0, sizeof(fds[cur_fds_count]));
- index--;
- }
- cur_fds_count_temp--;
- }
- }
- }
- return 0;
- }
如此,我们便将简单的poll服务器给写完了。我们看下poll模型的处理能力。采用和《朴素、Select、Poll和Epoll网络编程模型实现和分析——朴素模型》一文中相同的环境和压力,我们看下服务器的数据输出
再看下客户端的输出
0 0
- 朴素、Select、Poll和Epoll网络编程模型实现和分析——Poll模型
- 朴素、Select、Poll和Epoll网络编程模型实现和分析——Poll模型
- 朴素、Select、Poll和Epoll网络编程模型实现和分析——朴素模型
- 朴素、Select、Poll和Epoll网络编程模型实现和分析——朴素模型
- 朴素、Select、Poll和Epoll网络编程模型实现和分析——Epoll模型
- 朴素、Select、Poll和Epoll网络编程模型实现和分析——Epoll模型
- 朴素、Select、Poll和Epoll网络编程模型实现和分析——Select模型
- 朴素、Select、Poll和Epoll网络编程模型实现和分析——Select模型
- 朴素、Select、Poll和Epoll网络编程模型实现和分析——模型比较
- 朴素、Select、Poll和Epoll网络编程模型实现和分析——模型比较
- 朴素、Select、Poll和Epoll网络编程模型实现和分析——Poll、Epoll模型处理长连接性能比较
- 朴素、Select、Poll和Epoll网络编程模型实现和分析——Poll、Epoll模型处理长连接性能比较
- linux网络编程 select,poll,epoll模型
- IO模型和Select/Poll/Epoll解析
- 网络编程中select模型和poll模型学习(linux)
- select、poll、epoll 网络模型比较
- 网络I/O模型select/poll/epoll
- select,poll和epoll分析
- POJ 3279 Fliptile 开关问题
- javase 用来接收activemq消息的两种方式
- 伍华聪架构思路
- 程序员技术练级攻略
- C语言中函数参数入栈的顺序
- 朴素、Select、Poll和Epoll网络编程模型实现和分析——Poll模型
- Leetcode 6. ZigZag Conversion
- activemq消息转换器MessageConverter
- java.lang.IllegalStateException: getOutputStream() has already been called for this response
- 2016年国内某知名互联网公司PHP面试题及答案
- 关于jffs2文件系统如何掉电保护
- 神经网络+深度学习+增强学习
- AHU-61 Lake Counting (并查集)
- VLAN测试小结