libevent簡單介紹以及使用(帶有伺服器和客戶端)
這兩天使用了下libevent,只使用了網路IO事件(EV_READ和 EV_WRITE),查閱了下libevent的介面文件,這裡做點筆記,並附上程式碼,開發環境是win7+vs2010
這裡只介紹需要用到的libevent的介面函式,更多介面函式請檢視libevent官方文件
如果想了解libevent的原理,請問度娘,或是自行研究libevent原始碼,或是檢視libevent原始碼深度剖析
參考文章 socket非同步程式設計--libevent的使用
這裡使用libevent-2.1.1-alpha,在編譯libevent時候,如果出現<sys/queue.h>,那麼把 libevent-2.1.1-alpha/compat 加入工程專案中,如果出現libcmt.lib編譯問題,則在工程屬性->通用屬性->連結器->輸入->忽略特定預設庫,新增libcmt.lib, 具體原因請問度娘
libevent 的函式總共大約在50個左右,常用結構體event_base、event等,網路常用的就是其中幾個新版本event_base_new、event_new、event_free、event_add、event_del、event_base_dispatch、 event_assign()等, 老版本的event_init、event_set,event_dispatch等, 新版本多數是帶有“_base_”的
event_base_new和event_init 都是初始化event_base結構的,區別在於前者是執行緒安全的、而後者是非執行緒安全的,後者在其官方說明中已經被標誌為過時的函式、且建議用前者代替。
event_init:新建並初始化event_base, 原來沒有event_base
event_set:相當於初始化event,沒有新建,原來存在event物件。
event_add: 設定事件超時時間, 0表示一直等待
event_del:從event_base物件關注的events中刪除一個event
event_dispatch:分發事件,將一直執行,直到沒有已經註冊的事件了,或者呼叫了event_base_loopbreak()或者event_base_loopexit()或者事件操作出錯等為止。
event_base_new:新建並初始化event_base
event_new: 新建並初始化event
event_free:與event_new對應使用
event_base_dispatch: 與event_dispatch相似,但event_dispatch是多執行緒不安全的,event_base_dispatch多執行緒安全
event_assign:與event_set相似,但event_assign是執行緒安全的,event_set執行緒不安全,其官方說明:此函式可能在以後的libevent版本中不相容,建議使用event_base_new。
這裡程式碼使用新版本的
1. event_base_new()新建並初始化 event_base
2. 編寫事件回撥函式FuncA
3. event_new新建並初始化 event物件, 還設定事件回撥函式FuncA
4. event_base_dispatch, 分發事件
5. 需要結束的時候 event_free event物件 event_base_free event_base物件
附上程式碼,程式碼下載
伺服器:
#include <event.h>
#include <WinSock2.h>
#pragma comment(lib, "libevent.lib")
#pragma comment(lib, "ws2_32.lib")
#define LINSTEN_PORT 5001
#define LISTEN_MAX 5
#define BUFFER_SIZE 1024
struct event_base *base = NULL;
struct sock_ev
{
struct event *read_ev;
struct event *write_ev;
char *buf;
sock_ev()
{
read_ev = NULL;
write_ev = NULL;
buf = new char[BUFFER_SIZE];
memset(buf, 0, BUFFER_SIZE);
}
~sock_ev()
{
delete[] buf;
}
};
void release_sock_cv(sock_ev *ev)
{
if (ev->read_ev)
{
if( 0 != event_del(ev->read_ev))
{
printf("del err\n");
}
event_free(ev->read_ev);
ev->read_ev = NULL;
}
if (ev->write_ev)
{
event_del(ev->write_ev);
event_free(ev->write_ev);
ev->write_ev = NULL;
}
free(ev);
}
void on_read(evutil_socket_t sock, short eventRead, void *arg)
{
sock_ev *ev = (sock_ev*)arg;
int nSize = recv(sock, (char*)ev->buf, BUFFER_SIZE, 0);
if (nSize == SOCKET_ERROR )
{
printf("client socket closed\n");
release_sock_cv(ev);
closesocket(sock);
return;
}
printf("%s\n", ev->buf);
char *p = "recv hello world!";
send(sock, p, strlen(p), 0);
}
void on_write(evutil_socket_t sock, short eventWrite, void *arg)
{
sock_ev *ev = (sock_ev*)arg;
printf("on_write\n");
}
void on_accept(int sock, short eventAccept, void *arg)
{
struct sockaddr_in cliAddr;
int nSize = sizeof(sockaddr_in);
SOCKET cliSock = accept(sock, (sockaddr*)&cliAddr, &nSize);
sock_ev *ev = new sock_ev;
struct event *eventRead = event_new(base, cliSock, EV_READ | EV_PERSIST, on_read, ev);
struct event *eventWrite = event_new(base, cliSock, EV_WRITE | EV_PERSIST, on_write, ev);
ev->read_ev = eventRead;
ev->write_ev = eventWrite;
event_add(eventRead, NULL);
event_add(eventWrite, NULL);
}
int _tmain(int argc, _TCHAR* argv[])
{
WSAData wsaData;
WSAStartup(MAKEWORD(2, 0), &wsaData);
SOCKET sockListen = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in srvAddr;
srvAddr.sin_addr.s_addr = INADDR_ANY;
srvAddr.sin_family = AF_INET;
srvAddr.sin_port = htons(LINSTEN_PORT);
if (bind(sockListen, (sockaddr*)&srvAddr, sizeof(sockaddr)) != 0)
{
printf("bind err = %d\n", WSAGetLastError());
return 0;
}
if(0 != listen(sockListen, LISTEN_MAX))
{
printf("listen err = %d\n", WSAGetLastError());
return 0;
}
base = event_base_new();
struct event *eventListen = event_new(base, sockListen, EV_READ | EV_PERSIST, on_accept, NULL);
event_add(eventListen, NULL);
event_base_dispatch(base);
closesocket(sockListen);
event_base_free(base);
WSACleanup();
return 0;
}
客戶端:
#include <event.h>
#define SERVER_PORT 5001
#define BUFFER_SIZE 1024
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "libevent.lib")
int _tmain(int argc, _TCHAR* argv[])
{
WSAData wsa;
WSAStartup(MAKEWORD(2, 0), &wsa);
struct sockaddr_in srv;
srv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
srv.sin_family = AF_INET;
srv.sin_port = htons(SERVER_PORT);
SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
if(0 != connect(sock, (sockaddr*)&srv, sizeof(sockaddr)))
{
printf("connect err = %d", WSAGetLastError());
return 0;
}
send(sock, "helloworld!", 11, 0);
char buf[BUFFER_SIZE];
memset(buf, 0, BUFFER_SIZE);
recv(sock, buf, BUFFER_SIZE, 0);
closesocket(sock);
WSACleanup();
return 0;
}
效果如下: