epoll服务器
来源:互联网 发布:英语文献翻译软件 编辑:程序博客网 时间:2024/05/18 02:57
#include <sys/epoll.h>#include <stdio.h>#include <sys/socket.h>#include <sys/types.h>#include <netinet/in.h>#include <arpa/inet.h>#include <unistd.h>#include <stdlib.h>#include <string.h>#define MAX_EPOLL_SIZE 100static void usage(const char* pro){ printf("%s [local_ip][local_port]\n", pro);}int startup(const char* _ip, int _port){ // chuangjian taojiezi int sock = socket(AF_INET, SOCK_STREAM, 0); if(sock < 0){ perror("socket"); exit(3); } // bangding struct sockaddr_in local; local.sin_family = AF_INET; local.sin_port = htons(_port); local.sin_addr.s_addr = inet_addr(_ip); if(bind(sock, (struct sockaddr*)&local, sizeof(local)) < 0){ perror("bind"); exit(4); } // jianting if(listen(sock, 10) < 0){ perror("listen"); } return sock;}int main(int argc, char* argv[]){ if(argc != 3) { usage(argv[0]); exit(1); } int listen_sock = startup(argv[1], atoi(argv[2])); int ret = epoll_create(200); if(ret < 0) { perror("epoll_create"); exit(5); } struct epoll_event ev; ev.data.fd = listen_sock; ev.events = EPOLLIN; struct epoll_event ev_array[MAX_EPOLL_SIZE]; int i = 0; if(epoll_ctl(ret, EPOLL_CTL_ADD, listen_sock, &ev) < 0) { perror("epoll_ctl"); exit(6); } while(1) { int key = epoll_wait(ret, ev_array, MAX_EPOLL_SIZE, 1000); switch(key) { case 0: { printf("no discriptor is ready\n"); break; } case -1: { perror("epoll_wait"); break; } default: { for(i = 0;i < key; ++i) { if((ev_array[i].data.fd == listen_sock) && (ev_array[i].events & EPOLLIN)) //listen_sock is ready { struct sockaddr_in client; socklen_t len = sizeof(client); int new_sock = accept(ev_array[i].data.fd, (struct sockaddr*)&client, &len); if(new_sock < 0) { perror("accept"); continue; } //add new_sock to red_black_tree printf("get a new client : %s %d\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port)); struct epoll_event new_ev; new_ev.data.fd = new_sock; new_ev.events = EPOLLIN; //| EPOLLOUT; if(epoll_ctl(ret, EPOLL_CTL_ADD, new_sock, &new_ev) < 0) { close(new_sock); perror("epoll_ctl"); } continue; } else if(ev_array[i].events & EPOLLIN) // other sock read_ting is ready { char buf[1024]; ssize_t s = read(ev_array[i].data.fd, buf, sizeof(buf)-1); if(s > 0) { buf[s] = 0; printf("client -> server :%s\n", buf); } else if(s == 0) { printf("client qute...\n"); epoll_ctl(ret, EPOLL_CTL_DEL, ev_array[i].data.fd, &ev_array[i]); close(ev_array[i].data.fd); continue; }else { perror("read"); continue; } } else if(ev_array[i].events & EPOLLOUT) // other sock write_ting is ready { char* pStr = "liaoyueling is a pig"; write(ev_array[i].data.fd, pStr, strlen(pStr)); epoll_ctl(ret, EPOLL_CTL_DEL, ev_array[i].data.fd, &ev_array[i]); close(ev_array[i].data.fd); continue; } }//for }//deault }//switch }//while return 0;}
阅读全文
0 0
- epoll服务器
- epoll服务器
- epoll服务器
- epoll服务器
- Epoll服务器
- epoll服务器
- epoll服务器
- linux服务器编程--EPOLL
- epoll服务器示例
- 并发服务器之epoll
- epoll服务器编程-demo
- epoll服务器开发详解
- epoll回显服务器
- Epoll服务器架构
- 服务器epoll初用
- linux下epoll服务器
- python epoll开发服务器
- Epoll模型服务器实现
- rename"提交待审批"的标签的解决方案
- VIM类似source insight的用法
- LINUX搭建远程VNC桌面
- 理解display:box 和 display:flex
- python获取文件路径下的完整文件名(包括扩展名)
- epoll服务器
- 深度学习之OCR(一)——多数字识别_Multi-digit Number Recognition
- 批量获取Word文档的文件名信息
- mysql常用语法
- linux less命令简介
- 从零开始学习音视频编程技术(31) Linux篇之hello world
- irp的异步完成_irp等待插入队列_删除队列_结束irp
- 洛谷 P3819 松江1843路
- 禁止微信下拉