如何通过 libevent 创建 tcp-client
Posted
技术标签:
【中文标题】如何通过 libevent 创建 tcp-client【英文标题】:How to create tcp-client by libevent 【发布时间】:2013-01-05 08:15:17 【问题描述】:您是否有示例链接,如何创建客户端到 echo-server? 我需要使用 libevent、bufervents。 此代码已连接,但不写入服务器:
int main()
struct event_base *base;
struct evbuffer* ev_buffer;
const char hostname[] = "localhost";
struct sockaddr_in sin;
struct hostent *h;
int port = 5555;
const char *cp;
int fd;
ssize_t n_written, remaining;
char buf[BUFERSIZE];
h = gethostbyname(hostname);
if (!h)
fprintf(stderr, "Couldn't lookup %s: %s", hostname, hstrerror(h_errno));
return ERR;
if (h->h_addrtype != AF_INET)
fprintf(stderr, "No ipv4 support, sorry.");
return ERR;
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
sin.sin_addr = *(struct in_addr*)h->h_addr;
base = event_base_new();
struct bufferevent *bev_cnn = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);
bufferevent_setcb(bev_cnn, NULL, NULL, event_callback, NULL);
int res = bufferevent_socket_connect(bev_cnn, &sin, sizeof(sin));
if ((ev_buffer = evbuffer_new()) == NULL)
printf("proxy output buffer allocation failed");
event_base_dispatch( base );
bufferevent_enable(backend[0].buf_ev, EV_READ|EV_WRITE);
struct evbuffer *proxy_output = bufferevent_get_output(buf_ev);
size_t bufsize = evbuffer_get_length(proxy_output);
res = evbuffer_add(buf_ev, "test\r\n", 6);
if (bufferevent_write_buffer(bev_cnn, proxy_output))
printf("Error sending data to backend ****\n");
int nbytes = evbuffer_remove(backend[0].buf_ev, buf, BUFERSIZE);
bufferevent_disable(backend[0].buf_ev, EV_READ|EV_WRITE);
return OK;
当我有错误时?如何创建 Tcp 客户端?
【问题讨论】:
【参考方案1】:这段代码的问题在于,一旦您调用event_base_dispatch ( base );
,您的程序就位于libevent 处理系统中。它永远不会越过那条线来执行它后面的指令。
您需要一种方法将您的指令传递到事件队列中以便处理它们。
【讨论】:
【参考方案2】:这是我的示例工作正常
#include <iostream>
#include <event2/event.h>
#include <event2/bufferevent.h>
#include <event2/buffer.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
int64_t total_bytes_read = 0;
int64_t total_messages_read = 0;
static void set_tcp_no_delay(evutil_socket_t fd)
int one = 1;
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof one);
static void timeoutcb(evutil_socket_t fd, short what, void *arg)
struct event_base *base = (struct event_base *)arg;
printf("timeout\n");
event_base_loopexit(base, NULL);
static void readcb(struct bufferevent *bev, void *ctx)
/* This callback is invoked when there is data to read on bev. */
struct evbuffer *input = bufferevent_get_input(bev);
struct evbuffer *output = bufferevent_get_output(bev);
++total_messages_read;
total_bytes_read += evbuffer_get_length(input);
/* Copy all the data from the input buffer to the output buffer. */
evbuffer_add_buffer(output, input);
static void eventcb(struct bufferevent *bev, short events, void *ptr)
if (events & BEV_EVENT_CONNECTED)
evutil_socket_t fd = bufferevent_getfd(bev);
set_tcp_no_delay(fd);
else if (events & BEV_EVENT_ERROR)
printf("NOT Connected\n");
int main(int argc, char **argv)
struct event_base *base;
struct bufferevent **bevs;
struct sockaddr_in sin;
struct event *evtimeout;
struct timeval timeout;
int i;
if (argc != 5)
fprintf(stderr, "Usage: client <port> <blocksize> ");
fprintf(stderr, "<sessions> <time>\n");
return 1;
int port = atoi(argv[1]);
int block_size = atoi(argv[2]);
int session_count = atoi(argv[3]);
int seconds = atoi(argv[4]);
timeout.tv_sec = seconds;
timeout.tv_usec = 0;
base = event_base_new();
if (!base)
puts("Couldn't open event base");
return 1;
char* message = (char*)malloc(block_size);
//for (i = 0; i < block_size; ++i)
// message[i] = i % 128;
memset(message,'A',block_size);
evtimeout = evtimer_new(base, timeoutcb, base);
evtimer_add(evtimeout, &timeout);
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(0x7f000001); /* 127.0.0.1 */
sin.sin_port = htons(port);
bevs = (struct bufferevent **)malloc(session_count * sizeof(struct bufferevent *));
for (i = 0; i < session_count; ++i)
struct bufferevent *bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);
bufferevent_setcb(bev, readcb, NULL, eventcb, NULL);
bufferevent_enable(bev, EV_READ|EV_WRITE);
evbuffer_add(bufferevent_get_output(bev), message, block_size);
if (bufferevent_socket_connect(bev,(struct sockaddr *)&sin, sizeof(sin)) < 0)
/* Error starting connection */
bufferevent_free(bev);
puts("error connect");
return -1;
bevs[i] = bev;
event_base_dispatch(base);
for (i = 0; i < session_count; ++i)
bufferevent_free(bevs[i]);
free(bevs);
event_free(evtimeout);
event_base_free(base);
free(message);
printf("%zd total bytes read\n", total_bytes_read);
printf("%zd total messages read\n", total_messages_read);
printf("%.3f average messages size\n",
(double)total_bytes_read / total_messages_read);
printf("%.3f MiB/s throughtput\n",
(double)total_bytes_read / (timeout.tv_sec * 1024 * 1024));
return 0;
【讨论】:
以上是关于如何通过 libevent 创建 tcp-client的主要内容,如果未能解决你的问题,请参考以下文章