多路复用io接口-epoll
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多路复用io接口-epoll相关的知识,希望对你有一定的参考价值。
多路复用io接口-epoll的使用.
服务端使用epoll接口处理多个客户的服务请求.
例:
服务端代码
myepoll.c
#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <sys/wait.h> #include <unistd.h> #include <arpa/inet.h> #include <fcntl.h> #include <sys/epoll.h> #include <sys/time.h> #include <sys/resource.h> #define MAXBUF 1024 #define MAXEPOLLSIZE 10000 /* * setnonblocking - 设置sockfd为非阻塞方式 */ int setnonblocking(int sockfd) { if (fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFD, 0)|O_NONBLOCK) == -1) { perror("fcntl"); return -1; } return 0; } /* * handle_message - 处理每个 socket 上的消息收发 */ int handle_message(int new_fd) { char buf[MAXBUF + 1]; int len; /* 开始处理每个新连接上的数据收发 */ bzero(buf, MAXBUF + 1); /* 接收客户端的消息 */ len = recv(new_fd, buf, MAXBUF, 0); //printf("[%s-%d]*********\n", __func__, __LINE__); if (len > 0) { printf ("%d接收消息成功:‘%s‘,共%d个字节的数据\n", new_fd, buf, len); } else { if (len < 0) { printf("消息接收失败!错误代码是%d,错误信息是‘%s‘\n", errno, strerror(errno)); } close(new_fd); return -1; } /* 处理每个新连接上的数据收发结束 */ return len; } int main(int argc, char **argv) { int listener, new_fd, kdpfd, nfds, n, ret, curfds; socklen_t len; struct sockaddr_in my_addr, their_addr; unsigned int myport, lisnum; struct epoll_event ev; struct epoll_event events[MAXEPOLLSIZE]; struct rlimit rt; //if ((argc >= 2) && isdigit(atoi(argv[1]))) if (argc >= 2) { myport = atoi(argv[1]); } else { myport = 7838; } printf("myport = %d\n", myport); //if ((argc >= 3) && isdigit(atoi(argv[2]))) if (argc >= 3) { lisnum = atoi(argv[2]); } else { lisnum = 2; } printf("linum = %d\n", lisnum); /* 设置每个进程允许打开的最大文件数 */ rt.rlim_max = rt.rlim_cur = MAXEPOLLSIZE; if (setrlimit(RLIMIT_NOFILE, &rt) == -1) { perror("setrlimit"); exit(1); } else { printf("[line-%d]设置系统资源参数成功!\n", __LINE__); } /* 开启 socket 监听 */ if ((listener = socket(PF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } else { printf("[line-%d]socket 创建成功!\n", __LINE__); } setnonblocking(listener); bzero(&my_addr, sizeof(my_addr)); my_addr.sin_family = PF_INET; my_addr.sin_port = htons(myport); if(argv[3]) { my_addr.sin_addr.s_addr = inet_addr(argv[3]); } else { my_addr.sin_addr.s_addr = INADDR_ANY; } if (bind(listener, (struct sockaddr *) &my_addr, sizeof(struct sockaddr)) == -1) { perror("bind"); exit(1); } else { printf("[line-%d]IP 地址和端口绑定成功\n", __LINE__); } if (listen(listener, lisnum) == -1) { perror("listen"); exit(1); } else { printf("[line-%d]开启服务成功!\n", __LINE__); } /* 创建 epoll 句柄,把监听 socket 加入到 epoll 集合里 */ kdpfd = epoll_create(MAXEPOLLSIZE); len = sizeof(struct sockaddr_in); ev.events = EPOLLIN | EPOLLET; ev.data.fd = listener; if (epoll_ctl(kdpfd, EPOLL_CTL_ADD, listener, &ev) < 0) { fprintf(stderr, "epoll set insertion error: fd=%d\n", listener); return -1; } else { printf("[line-%d]监听 socket 加入 epoll 成功!\n", __LINE__); } curfds = 1; while(1) { /* 等待有事件发生 */ nfds = epoll_wait(kdpfd, events, curfds, -1); if (nfds == -1) { perror("epoll_wait"); break; } /* 处理所有事件 */ for (n = 0; n < nfds; ++n) { if (events[n].data.fd == listener) { new_fd = accept(listener, (struct sockaddr *) &their_addr, &len); if (new_fd < 0) { perror("accept"); continue; } else { printf("有连接来自于: %s:%d, 分配的 socket 为:%d\n", inet_ntoa(their_addr.sin_addr), ntohs(their_addr.sin_port), new_fd); } setnonblocking(new_fd); ev.events = EPOLLIN | EPOLLET; ev.data.fd = new_fd; if (epoll_ctl(kdpfd, EPOLL_CTL_ADD, new_fd, &ev) < 0) { fprintf(stderr, "把 socket ‘%d‘ 加入 epoll 失败!%s\n", new_fd, strerror(errno)); return -1; } curfds++; } else { ret = handle_message(events[n].data.fd); if (ret < 1 && errno != 11) { epoll_ctl(kdpfd, EPOLL_CTL_DEL, events[n].data.fd, &ev); curfds--; } } } } close(listener); return 0; }
客户端代码:
client.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <linux/in.h> #include <unistd.h> /* send data as "send ipaddr port message" */ int main(int argc, char *argv[]) { int sock_fd; struct sockaddr_in client_addr; struct sockaddr_in server_addr; char buf[1024]; int len; int ret; if(4 != argc) { printf("argc\n"); exit(1); } if(-1 == (sock_fd = socket(AF_INET, SOCK_STREAM, 0))) { printf("socket\n"); exit(2); } server_addr.sin_family = AF_INET; server_addr.sin_port = htons(atoi(argv[2])); server_addr.sin_addr.s_addr = inet_addr(argv[1]); if(-1 == (ret = connect(sock_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)))){ perror("connect"); goto ERR; } strcpy(buf, argv[3]); len = strlen(argv[3]); printf("[%s-L%d]\n", __func__, __LINE__); #if 1 if(-1 == (ret = send(sock_fd, buf, len, 0))){ perror("send: "); } #endif printf("[%s-L%d]\n", __func__, __LINE__); getchar(); ERR: close(sock_fd); }
编译链接, 在两个不同的终端运行,输出如下:
服务端
客户端. 按如下顺序在不同的终端执行, 并输出结果(上图服务端的输出也对应如下客户端执行顺序):
以上是关于多路复用io接口-epoll的主要内容,如果未能解决你的问题,请参考以下文章