0

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?

lily
  • 515
  • 7
  • 20

1 Answers1

1

The is all back to front. Sockets are almost always writeable. So you should:

  • send when you have data to send
  • call each send in a loop until t either completed or returns zero
  • if it returned zero, then select on writeability, and resume sending when it fires, and then cease selecting on writeability.
user207421
  • 305,947
  • 44
  • 307
  • 483
  • are there any sample codes for the second item (call each send in a loop until t either completed or returns zero)? I don't understand your third item (if it returned zero, then select on writeability, and resume sending when it fires, and then cease selecting on writeability.) what is " select on writeability"? thank you – lily Oct 18 '15 at 23:27
  • Select on writability is what your pseudocode is already doing. Examples of (2) abound. – user207421 Oct 19 '15 at 07:57