linux socket的IO多路复用简单例子(四)
2016-06-13 21:25
537 查看
使用epoll函数编写socket。
原理:使用一个文件描述符来管理多个描述符。epoll只拷贝一次描述符到内核态,当监控的事件发生时,通过函数回调将fd加入到一个就绪表中。接着,检测某个fd是否在就绪表中,存在的话进行对应的读写操作。epoll引入了一个事件结构体。每一个文件描述符对应一个事件结构体。结构体包含文件描述符,需要监控的事件。
server端:
参考:
http://www.cnblogs.com/Anker/p/3263780.html
原理:使用一个文件描述符来管理多个描述符。epoll只拷贝一次描述符到内核态,当监控的事件发生时,通过函数回调将fd加入到一个就绪表中。接着,检测某个fd是否在就绪表中,存在的话进行对应的读写操作。epoll引入了一个事件结构体。每一个文件描述符对应一个事件结构体。结构体包含文件描述符,需要监控的事件。
server端:
#include <iostream> #include <stdio.h> #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <string.h> #include <stdlib.h> #include <sys/shm.h> #include <sys/epoll.h> #include <vector> using namespace std; int main() { int server_fd = socket(AF_INET, SOCK_STREAM, 0); if (server_fd == -1) { perror("create server fd error"); return 0; } struct sockaddr_in serverAttr; serverAttr.sin_family = AF_INET; serverAttr.sin_port = htons(5875); serverAttr.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(server_fd, (struct sockaddr*)&serverAttr, sizeof(serverAttr)) == -1) { perror("bind error"); return 0; } if (listen(server_fd, 10)) { perror("listen error"); return 0; } /* Creates an epoll instance. Returns an fd for the new instance. The "size" parameter is a hint specifying the number of file descriptors to be associated with the new instance. The fd returned by epoll_create() should be closed with close(). int epoll_create (int __size) __THROW; */ //创建一个总的文件描述符 int epollFd = epoll_create(1024); //创建服务器fd所对应的事件结构体 struct epoll_event event; event.events = EPOLLIN; event.data.fd = server_fd; /* Manipulate an epoll instance "epfd". Returns 0 in case of success, -1 in case of error ( the "errno" variable will contain the specific error code ) The "op" parameter is one of the EPOLL_CTL_* constants defined above. The "fd" parameter is the target of the operation. The "event" parameter describes which events the caller is interested in and any associated user data. int epoll_ctl (int __epfd, int __op, int __fd, struct epoll_event *__event) __THROW; */ //将服务器fd和事件结构体增加到总的fd中 epoll_ctl(epollFd, EPOLL_CTL_ADD, server_fd, &event); //用来保存系统监控发生后,已经改变的fd事件 struct epoll_event evs[1024]; while (1) { /* Wait for events on an epoll instance "epfd". Returns the number of triggered events returned in "events" buffer. Or -1 in case of error with the "errno" variable set to the specific error code. The "events" parameter is a buffer that will contain triggered events. The "maxevents" is the maximum number of events to be returned ( usually size of "events" ). The "timeout" parameter specifies the maximum wait time in milliseconds (-1 == infinite). int epoll_wait (int __epfd, struct epoll_event *__events, int __maxevents, int __timeout); */ //ret表示发生改变的事件数量 int ret = epoll_wait(epollFd, evs, 100, -1); for (int i = 0; i < ret; i++) { int fd = evs[i].data.fd; //表示接受到新的请求 if ((fd == server_fd) && (evs[i].events & EPOLLIN)) { struct sockaddr_in clAttr; socklen_t len = sizeof(clAttr); int clFd = accept(server_fd, (struct sockaddr*)&clAttr, &len); if (clFd == -1) { perror("accept error"); continue; } cout << "accept client :" << inet_ntoa(clAttr.sin_addr) << ":" << clAttr.sin_port << endl; //将新的客户端fd和对应的事件添加到总的fd中 struct epoll_event e; e.events = EPOLLIN; e.data.fd = clFd; epoll_ctl(epollFd, EPOLL_CTL_ADD, clFd, &e); continue; } //表示这个fd接受到客户端发送的消息 if (evs[i].events & EPOLLIN) { char buf[1024] = {0}; int nread = read(evs[i].data.fd, buf, 1024); if (nread == -1) { perror("read error"); close(fd); epoll_ctl(epollFd, EPOLL_CTL_DEL, evs[i].data.fd, &evs[i]); continue; } if (nread == 0) { perror("server close"); close(fd); epoll_ctl(epollFd, EPOLL_CTL_DEL, evs[i].data.fd, &evs[i]); continue; } cout << "read msg is " << buf << endl; evs[i].events = EPOLLOUT; epoll_ctl(epollFd, EPOLL_CTL_MOD, evs[i].data.fd, &evs[i]); continue; } //返回信息给对应的客户端 if (evs[i].events & EPOLLOUT) { string wrtBuf = "hello,clients[i]ient " + to_string(i); int nwrite = write(evs[i].data.fd, wrtBuf.c_str(), 1024); if (nwrite == -1) { perror("write error"); close(evs[i].data.fd); epoll_ctl(epollFd, EPOLL_CTL_DEL, evs[i].data.fd, &evs[i]); continue; } evs[i].events = EPOLLIN; epoll_ctl(epollFd, EPOLL_CTL_MOD, evs[i].data.fd, &evs[i]); } } } return 0; }
参考:
http://www.cnblogs.com/Anker/p/3263780.html
相关文章推荐
- linux awk命令使用
- linux平台下的Notepadqq安装
- linux 查看进程 和 杀死进程
- linux socket的IO多路复用简单例子(三)
- linux_facl
- linux_压缩文件及目录
- Linux用户、组和权限
- Linux基本命令【一】
- 【操作系统】CentOS 7 ftp 使用教程
- linux socket的IO多路复用简单例子(二)
- Linux下JDK安装
- Linux基础命令
- Linux 下 ascii 的查看方式
- linux 下安装安装rz/sz命令
- linux socket的IO多路复用简单例子(一)
- linux下安装postgresql
- 每日一linux命令(12)-------more
- Linux技巧汇总
- Linux 的文件权限与目录配置
- linux下面select返回115错误