I am working through some examples on libevent and am running into a problem with reading any string over 8 bytes from the input buffer. I'm going between two computers on my local network. Here is my code for the simple libevent server:
#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;
}
This program continues to run while on another computer on the local network I run a simple client program using regular networking:
#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;
}
I run this simple client program, which should write to the simple server made using evbuffer and output the data to stdout
. The server writes to stdout
:
> We got some data: Messagef
So data
is only 8 bytes, the rest of the string gets truncated. Can anyone tell me why?
You are passing the size of a pointer (which happens to be 8 on your 64-bit platform) here:
int n = send(SOCKET, s, sizeof(s), 0);
^^^^^^^^^
You need to instead pass the number of characters in the string (+ 1 to include the NUL terminator):
int n = send(SOCKET, s, strlen(s) + 1, 0);
^^^^^^^^^^^^^