1

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.

Tom
  • 99
  • 7
  • C and C++ tags are mutually exclusive, please fix that. As a new user here, also take the [tour] and read [ask]. – Ulrich Eckhardt Jul 12 '22 at 04:32
  • Not familiar with this library, but you should check the return value from `bufferevent_write` to see if it's trying to send you a message like, "Slow the down! I'm ing full!" – user4581301 Jul 12 '22 at 05:02
  • I add the debug info ,bufferevent_write return the same value `0` – Tom Jul 12 '22 at 05:10

1 Answers1

0

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
        }
   } 


asele
  • 52
  • 6