用poll 实现并发服务器
来源:互联网 发布:djvu阅读器 知乎 编辑:程序博客网 时间:2024/04/28 04:23
poll函数类似于select函数,但是它的程序员接口不同,尽管它也可用于任何类型 文件描述符。与select不同,poll不为每个状态(可读,可写,异常状态)构造一个描述符集,而是构造一个pollfd结构数组,每个数组元素指定一个描述符编号以及对其所关心的状态。
- struct pollfd {
- int fd; //file descriptor to check
- short events; //events of interest on fd
- short revents; //events that occured on fd
- };
struct pollfd {int fd;//file descriptor to checkshort events;//events of interest on fdshort revents;//events that occured on fd};同样实现一个TCP服务器看看。
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<sys/types.h>
- #include<sys/socket.h>
- #include<netinet/in.h>
- #include<arpa/inet.h>
- #include<sys/poll.h>
- #include<limits.h>
- #define MAX_LISTEN 5
- #define PORT 1987
- #define IP "127.0.0.1"
- #define MAX_POLL 100
- //#define POLLRDNORM 0x0040
- int main()
- {
- int conn_fd;
- int sock_fd = socket(AF_INET,SOCK_STREAM,0);
- if (sock_fd < 0) {
- perror("create socket failed");
- exit(1);
- }
- struct sockaddr_in addr_client;
- int client_size = sizeof(struct sockaddr_in);
- struct sockaddr_in addr_serv;
- memset(&addr_serv, 0, sizeof(addr_serv));
- addr_serv.sin_family = AF_INET;
- addr_serv.sin_port = htons(PORT);
- addr_serv.sin_addr.s_addr = inet_addr(IP);
- if (bind(sock_fd,(struct sockaddr *)&addr_serv,sizeof(struct sockaddr_in)) < 0) {
- perror("bind error");
- exit(1);
- }
- if (listen(sock_fd,MAX_LISTEN) < 0) {
- perror("listen failed");
- exit(1);
- }
- //
- int recv_num;
- int send_num;
- char recv_buf[100];
- char send_buf[100];
- //initial "poll" elements
- int i, ready;
- struct pollfd client[MAX_POLL];
- client[0].fd = sock_fd;
- client[0].events = POLLRDNORM;
- for (i = 1;i < MAX_POLL; i ++) {
- client[i].fd = -1;
- }
- int maxi = 0;
- while (1) {
- ready = poll(client, maxi + 1, 0);
- //create new connection
- if (client[0].revents & POLLRDNORM) {
- conn_fd = accept(sock_fd, (struct sockaddr *)&addr_client, &client_size);
- if (conn_fd < 0) {
- perror("accept failed");
- exit(1);
- }
- for (i = 1;i < MAX_POLL;i ++) {
- if (client[i].fd < 0) {
- client[i].fd = conn_fd;
- break;
- }
- }
- if (i == MAX_POLL) {
- perror("too many clients");
- exit(1);
- }
- if (i > maxi)
- maxi = i;
- }
- //send and receive
- for (i = 1; i <= maxi; i ++) {
- if (client[i].fd >= 0 && (client[i].revents & (POLLRDNORM | POLLERR))) {
- recv_num = recv(client[i].fd, recv_buf, sizeof(recv_buf), 0);
- if (recv_num <= 0) {
- close(client[i].fd);
- client[i].fd = -1;
- } else {
- recv_buf[recv_num] = '\0';
- memset(send_buf,0,sizeof(send_buf));
- sprintf(send_buf, "server proc got %d bytes\n", recv_num);
- send_num = send(client[i].fd, send_buf, strlen(send_buf), 0);
- if (send_num <= 0) {
- close(client[i].fd);
- client[i].fd = -1;
- }
- }
- }
- }
- }
- close(sock_fd);
- return 0;
- }
- 用poll 实现并发服务器
- 并发服务器poll
- 实现TCP并发服务器之四(poll函数)
- Linux网络编程——tcp并发服务器(poll实现)
- 几种并发服务器模型的实现:多线程,多进程,select,poll,epoll - rail
- Linux网络编程——tcp并发服务器(poll实现)
- Linux网络编程——tcp并发服务器(poll实现)
- Linux网络编程——tcp并发服务器(poll实现)
- Linux Socket 多并发服务器开源代码:xSocketd 实现PPC/TPC/SELECT/POLL/EPOLL
- 几种并发服务器模型的实现:多线程,多进程,select,poll,epoll
- 2高并发服务器:多路IO之poll
- select和poll服务器实现(Linux)
- poll服务器
- poll服务器
- poll服务器
- poll服务器
- poll服务器
- poll服务器
- JQuery blockUI 的使用方法
- 读入两个小于100的正整数A和B,计算A+B.A和B的每一位数字由对应的英文单词给出.
- 用我自己的理解内存映射文件
- 百度面试
- ubantu 命令行 sh 无法找到 /bin/java
- 用poll 实现并发服务器
- STL set 用法之 —— 自定义结构体
- hadoop1.2.1编译eclipse插件
- Box2D源码学习(4)-Common目录下的其他类简介
- oracle db_ultra_safe Parameter
- 设置mysql允许外网访问
- Ubuntu 13.10 上手
- 【书籍】书籍推荐
- MySQl数据库允许远程访问