Linux高性能服务器之多路转接(2)---poll模型
来源:互联网 发布:网络教育退学 编辑:程序博客网 时间:2024/06/14 07:13
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <string.h> #include <poll.h> #include <sys/socket.h> #include <sys/types.h> #include <sys/stat.h> #define POLLFD_SIZE 1024 /* struct pollfd 结构体数组最大上限 */ struct pollfd array_pollfd[POLLFD_SIZE]; /* 结构体成员详情 struct pollfd { int fd; // 关心的描述符 short events; // 关心的事件 short revents; // 发生的事件 }; */ int startup(const char* ip, int port) { int sock = socket(AF_INET, SOCK_STREAM, 0); if( sock < 0){ perror("socket()"); exit(1); } int opt = 1; setsockopt(sock , SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); struct sockaddr_in local; local.sin_addr.s_addr = inet_addr(ip); local.sin_port = htons(port); local.sin_family = AF_INET; if(bind(sock, (struct sockaddr *)&local, sizeof(local) ) < 0){ perror("bind()"); exit(2); } if(listen(sock, 5) < 0){ perror("listen()"); exit(3); } return sock; } void run_poll(int listen_sockfd); void run_accept(int listen_sock); void run_action( int index); int main(int argc, char **argv) { if(argc != 3) { printf("usage: %s[local_ip][local_port]", argv[0]); return 1; } int listen_sockfd = startup(argv[1], atoi(argv[2])); array_pollfd[0].fd = listen_sockfd; array_pollfd[0].events = POLLIN; int idx_init = 1; for(; idx_init < POLLFD_SIZE; ++idx_init) { array_pollfd[idx_init].fd = -1; } int timeout = 1000; while(1) { int ret_poll = poll(array_pollfd, POLLFD_SIZE, timeout); if(ret_poll == 0) printf("timeout\n"); else if(ret_poll < 0) perror("poll()"); else { int idx_check = 0; for(idx_check = 0; idx_check < POLLFD_SIZE; ++idx_check) { if(idx_check == 0 && array_pollfd[0].revents & POLLIN) { run_accept(listen_sockfd); } else if(idx_check != 0) { run_action(idx_check); } } } } return 0; } void run_action( int index) { if(array_pollfd[index].revents & POLLIN) { char buf[1024]; memset(buf, 0, sizeof(buf)); ssize_t s = read(array_pollfd[index].fd, buf, sizeof(buf)-1); if(s > 0) { buf[s-1] = '\0'; printf("client say$ %s \n", buf); array_pollfd[index].events = POLLOUT; } else if( s <= 0) { printf("client quit!\n"); close(array_pollfd[index].fd); array_pollfd[index].fd = -1; } } else if (array_pollfd[index].revents & POLLOUT) { const char* msg = "HTTP/1.1 200 OK\r\n\r\n<html><br/><h1>Hello poll!</h1></html>"; write(array_pollfd[index].fd, msg, strlen(msg)); close(array_pollfd[index].fd); array_pollfd[index].fd = -1; } } void run_accept(int listen_sock) { struct sockaddr_in cliaddr; socklen_t clilen = sizeof(cliaddr); int new_sock = accept(listen_sock, (struct sockaddr*)&cliaddr, &clilen); if( new_sock < 0) { perror("accept"); return ; } printf("与客户端连接成功: ip %s port %d \n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port)); int idx_find = 1; for(; idx_find < POLLFD_SIZE; ++idx_find) { if(array_pollfd[idx_find].fd < 0) { array_pollfd[idx_find].fd = new_sock; array_pollfd[idx_find].events = POLLIN ; break; } } if(idx_find == POLLFD_SIZE) { perror("连接超出最大限度,add array_pollfd[]"); return; } }
阅读全文
0 0
- Linux高性能服务器之多路转接(2)---poll模型
- Linux高性能服务器之多路转接(1)----select模型实现
- Linux网络编程【七】:TCP协议高性能服务器(http)模型之I/O多路转接poll
- 【Linux网络编程】基于TCP流 I/O多路转接(poll) 的高性能http服务器
- 【Linux网络编程】I/O多路转接之 epoll 高性能简洁http服务器模型
- (五十四)高并发服务器——多路IO转接机制poll模型
- Linux网络编程【五】:TCP协议高性能服务器(http)模型之I/O多路转接select
- 【Linux网络编程】基于TCP协议 I/O多路转接(select) 的高性能回显服务器客户端模型
- Linux网络编程【六】:TCP协议高性能服务器(http)模型之I/O多路转接epoll
- 高级I/O之多路转接poll
- Linux I/O多路转接----poll模型
- linux高性能服务器编程之多进程
- 多路转接模型之poll
- 多路转接服务器之poll
- 【Linux】I/O多路转接poll
- Linux中poll实现高性能服务器及优缺点总结
- Linux【网络编程】——I/O多路转接之Poll服务器
- Linux下高级I/O多路转接之poll服务器
- ormlite-android使用
- js百度地图marker与swiper轮播图联动
- 004-谈一谈lock和synchronized
- Cannot find module 'angular-in-memory-web-api'
- centos安装caffe全过程
- Linux高性能服务器之多路转接(2)---poll模型
- 一个exe是32位的,还是64位的?
- Javascript 判断数据的类型
- Java StringTokenizer示例
- 多线程同步卖票demo
- 如何用代码将一个uiview设置到最前面显示 [superview bringSubviewToFront:subview]
- 在嵌入式平台中利用alsa dmix实现混音
- MD5字符串加密
- Django 学习