Currently, I use libevent
to send and receive messages. The issue I am currently encountering is that I do not receive all messages on the server; and only receive the first message I sent.
Client Code:
for (int i=0; i < 10 ; i++)
{
bufferevent_write(bev, data, strlen(data) + 1);
}
Server Code:
static void read_cb(struct bufferevent* bev, void* arg)
{
char buf[2048] = {};
bufferevent_read(bev, buf, sizeof(buf));
//do something
}
I have modified the client like this:
for (int i=0; i < 10 ; i++)
{
bufferevent_write(bev, data, strlen(data) + 1);
sleep(1)
}
When I add a sleep(1)
,I can receive all messages.
I would like to avoid using sleep(1)
. What needs to be added / changed in code, such that all messages can be received, and sleep
is not used.
This situation seems to be a bit like a spacket splicing problem, you can try this
static void read_cb(struct bufferevent* bev, void* arg)
{
char bufs[2048];
struct evbuffer *input = bufferevent_get_input(bev);
size_t lens = evbuffer_get_length(input);
char * rline = bufs;
while( lens > 0){
char buf [1024]
memcpy(buf, rline, strlen(data));
rline = rline + strlen(data);
lens = lens -strlen(data);
// use buf do something else
}
}