注意使用时添加windows的网络库
#include<iostream>
#include "event2/event.h"
#include "event2/listener.h"
void cb(struct evconnlistener*, evutil_socket_t, struct sockaddr*, int socklen, void*)
{
std::cout << "evconnlistener_cb 接受到连接De回调" << std::endl;
}
int main()
{
#ifdef _WIN32
WSADATA wver = {0};
int a = WSAStartup(MAKEWORD(2, 2), &wver);
#endif // _WIN32
//创建libevent的上下文
event_base* base = event_base_new();
if (base)
std::cout << "成功" << std::endl;
else
std::cout << "失败" << std::endl;
//监听端口
//socket,bind,listen
sockaddr_in sin = { 0 };
sin.sin_family = AF_INET;
sin.sin_port = htons(5001);
evconnlistener* ev = evconnlistener_new_bind(base,
cb,//回调函数
base,//用户自定义参数
LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_FREE, //地址重用,listen关闭同时清理socket
10,//连接队列大小,等同于listen函数
(sockaddr*)&sin,//绑定地址和端口
sizeof(sin)
);
//事件分发
event_base_dispatch(base);
evconnlistener_free(ev);
event_base_free(base);
return 0;
}