I am trying to throttle my network of only one receiver.
Firstly,I detect the size of SO_SNDBUF by :
getsockopt(sendsockfd, SOL_SOCKET, SO_SNDBUF, &sndBufferSize, &sbsLen);
Then, I keep filling up this buffer until it is entirely full by :
if (sndBufferSize - NbBytesInBuffer >= HEADER){
memcpy (sendBuffer + NbBytesInBuffer, &buf_header, HEADER);
NbBytesInBuffer +=HEADER;
}
Ofcourse, I am mentioning just the relevant part of my code.
Finally, when the buffer is full, write to socket.
if (sndBufferSize - NbBytesInBuffer < HEADER)
sentSize = write(sendsockfd,sendBuffer,NbBytesInBuffer);
...
My problem is not mentioned yet. All the above stuff are working perfectly until I want to resend data again.
Now, because I want to send in the maximum possible rate, I have to wait as less as possible. (i.e. I have to resend right away the send buffer is empty).
How to detect the needed time for write()
in order to re-empty the sending buffer (in the most optimum way)?
P.S. Please don't tell me to wait arbitrary time (for instance usleep(10000);
)
If you want to send at the maximum rate, write
should block by default when needed. As soon as write
returns, it's done with your sendBuffer
and you can put the next block of data into it and call write
again.
You only have to mess with select
if you set up the socket as nonblocking, and in that case the condition you're waiting for is that the socet is writable.