gpt4 book ai didi

c - 如何通过libevent创建tcp客户端

转载 作者:行者123 更新时间:2023-11-30 15:52:46 26 4
gpt4 key购买 nike

您是否有示例链接,如何创建回显服务器的客户端? 我需要使用 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 客户端?

最佳答案

这是我的示例,工作正常

#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;
}

关于c - 如何通过libevent创建tcp客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14169971/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com