设为首页 加入收藏

TOP

在Windows环境下实现一个简单的libevent服务器(二)
2017-10-10 12:02:52 】 浏览:9618
Tags:Windows 环境 实现 一个 简单 libevent 服务器
*arg); 22 //error回调函数 23 void error_cb(struct bufferevent *bev, short event, void *arg); 24 //write 回调函数 25 void write_cb(struct bufferevent *bev, void *arg); 26 /********************************************************************************* 27 * 函数体 28 **********************************************************************************/ 29 //accept回掉函数 30 void do_accept_cb(evutil_socket_t listener, short event, void *arg) 31 { 32 //传入的event_base指针 33 struct event_base *base = (struct event_base*)arg; 34 //socket描述符 35 evutil_socket_t fd; 36 //声明地址 37 struct sockaddr_in sin; 38 //地址长度声明 39 socklen_t slen = sizeof(sin); 40 //接收客户端 41 fd = accept(listener, (struct sockaddr *)&sin, &slen); 42 if (fd < 0) 43 { 44 perror("error accept"); 45 return; 46 } 47 printf("ACCEPT: fd = %u\n", fd); 48 ////注册一个bufferevent_socket_new事件 49 struct bufferevent *bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE); 50 ////设置回掉函数 51 bufferevent_setcb(bev, read_cb, NULL, error_cb, arg); 52 ////设置该事件的属性 53 bufferevent_enable(bev, EV_READ | EV_WRITE | EV_PERSIST); 54 } 55 ////read 回调函数 56 void read_cb(struct bufferevent *bev, void *arg) 57 { 58 #define MAX_LINE 256 59 char line[MAX_LINE + 1]; 60 int n; 61 //通过传入参数bev找到socket fd 62 evutil_socket_t fd = bufferevent_getfd(bev); 63 // 64 while (n = bufferevent_read(bev, line, MAX_LINE)) 65 { 66 line[n] = '\0'; 67 printf("fd=%u, read line: %s\n", fd, line); 68 //将获取的数据返回给客户端 69 bufferevent_write(bev, line, n); 70 } 71 } 72 ////error回调函数 73 void error_cb(struct bufferevent *bev, short event, void *arg) 74 { 75 //通过传入参数bev找到socket fd 76 evutil_socket_t fd = bufferevent_getfd(bev); 77 //cout << "fd = " << fd << endl; 78 if (event & BEV_EVENT_TIMEOUT) 79 { 80 printf("Timed out\n"); //if bufferevent_set_timeouts() called 81 } 82 else if (event & BEV_EVENT_EOF) 83 { 84 printf("connection closed\n"); 85 } 86 else if (event & BEV_EVENT_ERROR) 87 { 88 printf("some other error\n"); 89 } 90 bufferevent_free(bev); 91 } 92 ////write 回调函数 93 void write_cb(struct bufferevent *bev, void *arg) 94 { 95 char str[50]; 96 //通过传入参数bev找到socket fd 97 evutil_socket_t fd = bufferevent_getfd(bev); 98 //cin >> str; 99 printf("输入数据!"); 100 scanf_s("%d", &str); 101 bufferevent_write(bev, &str, sizeof(str)); 102 } 103 104 int main() 105 { 106 int ret; 107 evutil_socket_t listener; 108 WSADATA Ws; 109 //Init Windows Socket 110 if (WSAStartup(MAKEWORD(2, 2), &Ws) != 0) 111 { 112 return -1; 113 } 114 listener = socket(AF_INET, SOCK_STREAM, 0); 115 assert(listener > 0); 116 evutil_make_listen_socket_reuseable(listener); 117 struct sockaddr_in sin; 118 sin.sin_family = AF_INET; 119 sin.sin_addr.s_addr = 0; 120 sin.sin_port = htons(LISTEN_PORT); 121 if (bind(listener, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 122 perror("bind"); 123 return 1; 124 } 125 if (listen(listener, 1000) < 0) { 126 perror("listen"); 127 return 1; 128 } 129 printf("Listening...\n"); 130 evutil_make_socket_nonblocking(listener); 131 struct event_base *base = event_base_new(); 132 assert(base != NULL); 133 struct event *listen_event; 134 listen_event = event_new(base, listener, EV_READ | EV_PE
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇php中数组和字符串的相互转换 下一篇PHPExcel 相关操作

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目