how to properly deal with send/write with a non-blocking socket?

I have a socket which is used to receive and also send packets. And the pseudo codes are:

setnonblock(fd);
add_event(event_base, recv_ev);
while("I have packets to write"){
send(fd, packet);
....
}

....


now the issue is, since fd is non-blocking, send(fd) many return before it finish sending packets. but I hope it can send the packet successfully before the program runs to the next step, or register an event for it. But if I register an event for it, the event may be triggered frequently even if there are no packets available (note the packets are not from recv() in the pseudo code ,but from somewhere else)

then how to deal with it?


Last edited on
The whole point of using non-blocking sockets is allow you to do stuff while the packet is being sent. If you have nothing to do between sends, you may as well use blocking sockets.

According to http://pubs.opengroup.org/onlinepubs/007908799/xns/send.html, "The select() and poll() functions can be used to determine when it is possible to send more data."

However, these functions are known to be slow for several reasons. So in practice, system specific calls are used.
Topic archived. No new replies allowed.