zoukankan      html  css  js  c++  java
  • libevent终于编译通过了

    在网上找了个例子,其实libevent本身带了很多测试用例,不过这是第一次编译成功,尼玛 各种高性能IO库都是在linux下的,win32果断被bs了,还好有libevent对win32支持的比较好。lib很顺利的编译好了。

    下面就是很简单的使用下socket:

    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <assert.h>
    #include <WS2tcpip.h>
    #include <windows.h>
    #include <winsock.h>
    #pragma comment(lib,"ws2_32.lib")
    
    #include <event2/event.h>
    #include <event2/bufferevent.h>
    
    #define LISTEN_PORT 9999
    #define LISTEN_BACKLOG 32
    
    void do_accept(evutil_socket_t listener, short event, void *arg);
    void read_cb(struct bufferevent *bev, void *arg);
    void error_cb(struct bufferevent *bev, short event, void *arg);
    void write_cb(struct bufferevent *bev, void *arg);
    
    int main(int argc, char *argv[])
    {
    	WSADATA Data; 
    	int status=WSAStartup(MAKEWORD(1, 1), &Data);  
    	if (status != 0)  
    		return 0;
    
    	int ret = 0;
    	evutil_socket_t listener;
    	listener = socket(AF_INET, SOCK_STREAM, 0);
    	assert(listener > 0);
    	evutil_make_listen_socket_reuseable(listener);
    
    	struct sockaddr_in sin;
    	sin.sin_family = AF_INET;
    	sin.sin_addr.s_addr = 0;
    	sin.sin_port = htons(LISTEN_PORT);
    
    	if (bind(listener, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
    		perror("bind");
    		return 1;
    	}
    
    	if (listen(listener, LISTEN_BACKLOG) < 0) {
    		perror("listen");
    		return 1;
    	}
    
    	printf ("Listening...
    ");
    
    	evutil_make_socket_nonblocking(listener);
    
    	struct event_base *base = event_base_new();
    	assert(base != NULL);
    	struct event *listen_event;
    	listen_event = event_new(base, listener, EV_READ|EV_PERSIST, do_accept, (void*)base);
    	event_add(listen_event, NULL);
    	event_base_dispatch(base);
    
    	printf("The End.");
    	return 0;
    }
    
    void do_accept(evutil_socket_t listener, short event, void *arg)
    {
    	struct event_base *base = (struct event_base *)arg;
    	evutil_socket_t fd;
    	struct sockaddr_in sin;
    	socklen_t slen;
    	fd = accept(listener, (struct sockaddr *)&sin, &slen);
    	if (fd < 0) {
    		perror("accept");
    		return;
    	}
    	if (fd > FD_SETSIZE) {
    		perror("fd > FD_SETSIZE
    ");
    		return;
    	}
    
    	printf("ACCEPT: fd = %u
    ", fd);
    
    	struct bufferevent *bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
    	bufferevent_setcb(bev, read_cb, NULL, error_cb, arg);
    	bufferevent_enable(bev, EV_READ|EV_WRITE|EV_PERSIST);
    }
    
    void read_cb(struct bufferevent *bev, void *arg)
    {
    #define MAX_LINE    256
    	char line[MAX_LINE+1];
    	int n;
    	evutil_socket_t fd = bufferevent_getfd(bev);
    
    	while (n = bufferevent_read(bev, line, MAX_LINE), n > 0) {
    		line[n] = '';
    		printf("fd=%u, read line: %s
    ", fd, line);
    
    		bufferevent_write(bev, line, n);
    	}
    }
    
    void write_cb(struct bufferevent *bev, void *arg) {}
    
    void error_cb(struct bufferevent *bev, short event, void *arg)
    {
    	evutil_socket_t fd = bufferevent_getfd(bev);
    	printf("fd = %u, ", fd);
    	if (event & BEV_EVENT_TIMEOUT) {
    		printf("Timed out
    "); //if bufferevent_set_timeouts() called
    	}
    	else if (event & BEV_EVENT_EOF) {
    		printf("connection closed
    ");
    	}
    	else if (event & BEV_EVENT_ERROR) {
    		printf("some other error
    ");
    	}
    	bufferevent_free(bev);
    }
    

      

  • 相关阅读:
    【Spring】9、Spring中的事件Event
    【Spring】8、Spring框架中的单例Beans是线程安全的么
    【Spring】7、拦截器HandlerInterceptor
    【Spring】5、利用自定义注解在SpringMVC中实现自定义权限检查
    【Spring】4、Spring中 @Autowired标签与 @Resource标签 的区别
    【Dubbo&&Zookeeper】2、 windows平台dubbo-admin管理平台搭建
    【Dubbo&&Zookeeper】4、 Java实现Dubbo服务提供者及消费者注册
    【Dubbo&&Zookeeper】3、Failed to read schema document 'http://code.alibabatech.com/schema/dubbo/dubbo.xsd'问题解决方法
    【Dubbo&&Zookeeper】1、Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
    【Spring】3、BeanFactory 和 ApplicationContext的区别
  • 原文地址:https://www.cnblogs.com/xiangshancuizhu/p/3171012.html
Copyright © 2011-2022 走看看