[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[Libevent-users] sending data with libevent works just sometimes



Hi
I am very new to libevent and even to mailing lists (creating this thread is probably the 20th try; I found no documentation on that; even the help-command does not describe anything directly). Please be patient if I don't describe my problem that well.

I post this question on stackoverflow before I knew this mailing list but I think that the mailing list is the better option to get support for libevent.

I am using libevent-2.1.3-alpha that I have compiled on my own because I needed the pretty new function evbuffer_copyout_from (to get to this point it took hours, but thats another story). Also I am using the bufferevent for my programs.

While developing it's very common that things work or they don't (at least for the present). My test program sends data to my server:

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

Running this program from terminal, it sends the data or not. So it does not work every time 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 around this behavior, because I have to send this package before I send another one given that the server responses with a package that I have to proceed before.

My client have a GUI and it have to communicate 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. And this simple could be the while-loop of my program.

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

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 and 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.
Regards, Andre
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.