Reputation: 65
I have a real-time system, so I using the non-blocking socket to send my data But there is happened that the socket buffer is full, so the send function's return value less than my data length.
If I save the return length and re-send, there is not different with blocking socket?
So can I get the socket buffer's remainder size? I can check it first, if it is enough then I call send, skip send else.
Thank you, all.
Upvotes: 1
Views: 2018
Reputation: 27582
Well there is a difference between blocking and non-blocking - if you experience a short write you don't block. That's the whole point of non-blocking. It give you an opportunity to do something more pressing while waiting for some buffer space to free up.
Your concern seems to be the repeated attempts to write a full message, that is, a form of polling. But a check of the bytes free in the buffer is the same thing, you are just substituting the call to the availability with the call to write. You really don't gain anything efficiency wise.
The commonplace solution to this is to use something like select
or poll
that monitors the socket descriptor for the ability to write (and least some) bytes. This allows you stop polling and foist some of the work off on the kernel to monitor the space availability for you.
That said, if you really want to check to see how much space is available there are usually work arounds that tend to somewhat platform specific, mostly ioctl
calls with various platform specific parameters like FIONWRITE
, SIOCOUTQ
, etc. You would need to investigate exactly what your platform provides. But, again, it is better to consider if this is really something you need in the first place.
Upvotes: 1
Reputation: 13750
If the asynchronous send
fails with EWOULDBLOCK
/EAGAIN
, no data is sent. You could then try to send something else, or wait until the buffer is free again.
Also see https://stackoverflow.com/questions/19391208/when-a-non-blocking-send-only-transfers-partial-data-can-we-assume-it-would-r - a related issue is discussed there.
Upvotes: 0