2

例へのリンクはありますか、クライアントをエコーサーバーに作成する方法は? 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 クライアントを作成するにはどうすればよいですか?

4

2 に答える 2

1

これは私の例です

#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;
}
于 2016-12-27T10:24:50.090 に答える
1

このコードの問題は、 を呼び出すevent_base_dispatch ( base );と、プログラムが libevent 処理システム内に置かれることです。それに続く命令を実行するために、その行を通過することはありません。

命令を処理するには、命令をイベント キューに渡す方法が必要です。

于 2013-12-30T17:38:12.637 に答える