您是否有链接到示例,如何创建客户端echo-server?我需要使用libevent, bubuffvents。这段代码有连接,但不写到服务器:
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, "testrn", 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客户端
这就是我的例子
#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("timeoutn");
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 Connectedn");
}
}
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 readn", total_bytes_read);
printf("%zd total messages readn", total_messages_read);
printf("%.3f average messages sizen",
(double)total_bytes_read / total_messages_read);
printf("%.3f MiB/s throughtputn",
(double)total_bytes_read / (timeout.tv_sec * 1024 * 1024));
return 0;
}
这段代码的问题在于,一旦调用了event_base_dispatch ( base );
,程序就位于libevent处理系统中。它永远不会越过这一行来执行它后面的指令。
您需要一种方法将您的指令传递到事件队列以便处理它们。