我正在研究一些关于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个字节,其余的字符串被截断。谁能告诉我为什么?
答案 0 :(得分:2)
您正在传递指针的大小(在64位平台上恰好为8):
int n = send(SOCKET, s, sizeof(s), 0);
^^^^^^^^^
您需要传递字符串中的字符数(+ 1以包含NUL终止符):
int n = send(SOCKET, s, strlen(s) + 1, 0);
^^^^^^^^^^^^^