NO IMAGE

基於libevent實現了一個簡單的echoclient。之前在網上看到的都是echoserver。這裡演示一下使用libevent進行客戶端程式設計的基本步驟。

先看程式碼:

#include "stdafx.h"
#include "event2/event.h"
#include "event2/util.h"
#define ECHO_PORT   8888
#define ECHO_SERVER "127.0.0.1"
struct echo_context{
struct event_base *base;
struct event *event_write;
struct event *event_read;
const char * echo_contents;
int echo_contents_len;
int recved;
};
void write_cb(evutil_socket_t sock, short flags, void * args)
{
struct echo_context *ec = (struct echo_context *)args; 
int ret = send(sock, ec->echo_contents, ec->echo_contents_len, 0);
printf("connected, write to echo server: %d\n", ret);
event_add(ec->event_read, 0);
}
void read_cb(evutil_socket_t sock, short flags, void * args)
{
struct echo_context *ec = (struct echo_context *)args; 
char buf[128];
int ret = recv(sock, buf, 128, 0);
printf("read_cb, read %d bytes\n", ret);
if(ret > 0)
{
ec->recved  = ret;
buf[ret] = 0;
printf("recv:%s\n", buf);
}
else if(ret == 0)
{
printf("read_cb connection closed\n");
event_base_loopexit(ec->base, NULL);
return;
}
if(ec->recved < ec->echo_contents_len)
{
event_add(ec->event_read, 0);
}
}
static evutil_socket_t make_tcp_socket()
{
int on = 1;
evutil_socket_t sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
evutil_make_socket_nonblocking(sock);
#ifdef WIN32
setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (const char *)&on, sizeof(on));
#else
setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&on, sizeof(on));
#endif
return sock;
}
static void echo_client(struct event_base *base)
{
evutil_socket_t sock = make_tcp_socket();
struct sockaddr_in serverAddr;
struct event * ev_write = 0;
struct event * ev_read = 0;
struct timeval tv={10, 0};
struct echo_context *ec = (struct echo_context*)calloc(1, sizeof(struct echo_context));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(ECHO_PORT);
#ifdef WIN32
serverAddr.sin_addr.S_un.S_addr = inet_addr(ECHO_SERVER);
#else
serverAddr.sin_addr.s_addr = inet_addr(ECHO_SERVER);
#endif
memset(serverAddr.sin_zero, 0x00, 8);
connect(sock, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
ev_write = event_new(base, sock, EV_WRITE, write_cb, (void*)ec);
ev_read = event_new(base, sock, EV_READ , read_cb, (void*)ec);
ec->event_write = ev_write;
ec->event_read = ev_read;
ec->base = base;
ec->echo_contents = strdup("echo client tneilc ohce\n");
ec->echo_contents_len = strlen(ec->echo_contents);
ec->recved = 0;
event_add(ev_write, &tv);
}
int _tmain(int argc, _TCHAR* argv[])
{
struct event_base * base = 0;
#ifdef WIN32
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(2, 2);
(void) WSAStartup(wVersionRequested, &wsaData);
#endif
base = event_base_new();
echo_client(base);
event_base_dispatch(base);
event_base_free(base);
return 0;
}

程式碼是在windows上實現的。

使用libevent進行客戶端程式設計的基本步驟,在《libevent實現http client》中已經提過,基本如下:

  1. 初始化event_base(後續要執行事件迴圈)
  2. 建立socket,設定為非同步,連線server
  3. 建立寫讀寫事件,先將寫事件加入事件迴圈
  4. 在寫事件回撥中向server端傳送請求並將讀事件加入事件迴圈
  5. 在讀事件回撥中處理資料,並根據資料是否讀取完畢決定是否繼續新增讀事件

結合本文的示例程式碼,通過event_base_new()建立event_base,在初始化客戶端socket(echo_client)之後,呼叫event_base_dispatch執行事件迴圈。

echo_client函式初始化了socket,設定為非阻塞模式,呼叫connect連線到echoserver,然後新增一個寫事件到事件迴圈中。當連線成功後,會觸發寫事件。這裡將write_cb作為寫事件的回撥,在呼叫event_new()時傳入。

write_cb()寫入一個字串,然後新增一個讀事件,在讀事件中讀取echoserver的迴應。

基本的過程就是這樣子了。這是個簡單的例子,更復雜的例子,基本流程也是一樣的。