eventbuffer 只能读取 8 个字节的输入?

eventbuffer can only read 8 bytes on input?

我正在处理一些关于 libevent 的示例,并且 运行 遇到了从输入缓冲区读取任何超过 8 个字节的字符串的问题。我在本地网络上的两台计算机之间移动。这是我的简单 libevent 服务器代码:

#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#include <event2/listener.h>
#include <event2/bufferevent.h>
#include <event2/buffer.h>

#define PORTNUM 8080

static void echo_read_cb(struct bufferevent *bev, void *ctx)
{
    struct evbuffer *input = bufferevent_get_input(bev);
    struct evbuffer *output = bufferevent_get_output(bev);

    size_t len = evbuffer_get_length(input);
    char *data = malloc(len);
    evbuffer_copyout(input, data, len);
    printf("We got some data: %s\n", data);
    free(data);
}

static void echo_event_cb(struct bufferevent *bev, short events, void *ctx)
{
    if (events & BEV_EVENT_ERROR) 
        perror("Error from bufferevent.");
    if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) {
        bufferevent_free(bev);
    }
}

static void accept_conn_cb(struct evconnlistener *listener, 
                           evutil_socket_t fd, struct sockaddr *addr,
                           int socklen, void *ctx)
{
    struct event_base *base = evconnlistener_get_base(listener);
    struct bufferevent *bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
    bufferevent_setcb(bev, echo_read_cb, NULL, echo_event_cb, NULL);
    bufferevent_enable(bev, EV_READ|EV_WRITE);
}

static void accept_error_cb(struct evconnlistener *listener, void *ctx) {
    struct event_base *base = evconnlistener_get_base(listener);
    int err = EVUTIL_SOCKET_ERROR();
    fprintf(stderr, "Got an error %d (%s) on the listener. "
            "Shutting down.\n", err, evutil_socket_error_to_string(err));
    event_base_loopexit(base, NULL);
}

int main(int argc, char **argv)
{
    struct event_base *base;
    struct evconnlistener *listener;
    struct sockaddr_in sin;

    base = event_base_new();
    if (!base) {
        puts("Couldn't open event base\n");
        return 1;
    }

    memset(&sin, 0, sizeof(sin));
    sin.sin_family = AF_INET;
    sin.sin_port = htons(PORTNUM);
    sin.sin_addr.s_addr = htonl(INADDR_ANY);

    listener = evconnlistener_new_bind(base, accept_conn_cb, NULL,
                                       LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE,
                                       -1, (struct sockaddr *)&sin, 
                                       sizeof(sin));
    if (!listener) {
        perror("Couldn't create listener.\n");
        return 1;
    }

    evconnlistener_set_error_cb(listener, accept_error_cb);

    event_base_dispatch(base);
    return 0;
}

此程序继续 运行 而在本地网络上的另一台计算机上我 运行 使用常规网络的简单客户端程序:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main()
{
    struct sockaddr_in serv;
    memset(&serv, 0, sizeof(serv));
    serv.sin_family = AF_INET;
    serv.sin_port = htons(8080);
    inet_aton("192.168.1.155", &(serv.sin_addr));

    int SOCKET = socket(AF_INET, SOCK_STREAM, 0);
    connect(SOCKET, (struct sockaddr *)&serv, sizeof(struct sockaddr_in));

    const char *s = "Messagefromtheclient.";
    int n = send(SOCKET, s, sizeof(s), 0);
    close(SOCKET);
    return EXIT_SUCCESS;
}

我运行这个简单的客户端程序,它应该写入使用evbuffer制作的简单服务器并将数据输出到stdout。服务器写入 stdout

> We got some data: Messagef

所以 data 只有 8 个字节,字符串的其余部分得到 t运行 处理。谁能告诉我为什么?

您在此处传递指针的大小(在您的 64 位平台上恰好为 8):

int n = send(SOCKET, s, sizeof(s), 0);
                        ^^^^^^^^^

您需要改为传递字符串中的字符数(+ 1 以包含 NUL 终止符):

int n = send(SOCKET, s, strlen(s) + 1, 0);
                        ^^^^^^^^^^^^^