0

While developing it's very common that things work or they don't. When sending data from my client to my server it does not work everytime but in most cases it does. I am guessing that probably the kernel don't send the buffer it has stored. Anyways, there have to be a method to work arround this behaviour.

My client have a GUI and it have to communitcate with a server. Because threads don't work as I want them to, I decided to use event_base_loop so that it just blocks until one package is processed. After that it can do GUI stuff so that the window won't freeze.

I am very certain that my sending fails and NOT my reading because my server does not call the my callback for reading ("readcb").

The attached function i am calling from the main function like this:

int main(int argc, char **argv)
{
    // init stuff
    // connnect to server
    sendPacket(bev);
}

I researched a lot about this, but I don't find anything. For example bufferevent_flush(bev, EV_WRITE, BEV_FLUSH) don't works with sockets (i tried it even out).

My current function for writing (in short form, simplified for one package):

void sendPacket(bufferevent * bev)
{
    // just data:
    const unsigned int msg_ = 12;
    char msg[msg_] = "01234567891";
    // send that packet:
    uint16_t packet_id = 1
    bufferevent_write(bev, &packet_id, 2);
    bufferevent_write(bev, msg, msg_);
    //and this part SHOULD make that data really send but it does not every time:
    while (evbuffer_get_length(bufferevent_get_output(bev)) > 0)
    {
        event_base_loop(bufferevent_get_base(bev), EVLOOP_ONCE);
    };
    //this last one, only to be really sure (that's why i use the second option):
    event_base_loop(bufferevent_get_base(bev), EVLOOP_NONBLOCK | EVLOOP_ONCE);
}

Thanks for your time, I would be lost without your help.

andrew
  • 97
  • 1
  • 10
  • `While developing it's very common that things work or they don't.` Actually it's far more common that they appear to "work" a random amount of the time because you have some runtime bug that is not explicitly diagnosed. – Lightness Races in Orbit Jan 06 '14 at 00:05
  • BTW, `main` returns `int`, always. – Lightness Races in Orbit Jan 06 '14 at 00:06
  • 1
    Make sure that you do this.. at least for a threaded environment. * m_LibEventInstance = event_base_new(); * evthread_use_windows_threads(); (or evthread_use_pthreads();) then finish all of your other initialization. This took me months to figure out. BTW, returning anything from a main, especially when you won't be able to get a valid return value (as in normal libevent->run()), is worthless. It may be the new standard, but you are probably not going to use the result.. so – Mickey Kawick Jan 06 '14 at 21:50

0 Answers0