× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



I've found that it's more robust to call read/recv first, and only call select() if read/recv returns that it "would block".

There are three reasons why this approach is better:

1) The same pattern can be used with SSL calls. Calling select() first is not valid in SSL because select waits on the raw (still encrypted) socket. It's possible for select() to wait for the network even though there's data in the SSL buffer available to return immediately. Data that has already been removed from the TCP channel, and decrypted and SSL has it in a buffer to be returned -- but since it's out of the TCP buffer, select does not know about it. I realize that this example did not use SSL, but I prefer to use the same pattern for both,

2) When data is available on the socket already, calling recv/read first is a bit more efficient, since there's already data and so no need for the call to select().

3) There used to be sporadic situations where select() would get stuck waiting when there's data in the TCP read buffer. I think all of these were fixed, they were bugs in the OS ages ago... but using this pattern worked around the problem.

What was the reason for calling select() first? As long as you are using non-blocking sockets, I have never seen a disadvantage to calling read/recv first...

-SK


On 9/17/2015 2:03 PM, Bradley Stone wrote:
select() should be called before the read()/recv() to know if the socket is
ready for the read (or write).

The timeout of 60 seconds seems quite big. This isn't a delay if that's
what you're thinking.

If you specify 60 seconds that means it will wait on the select() for up to
60 seconds for "readiness" and then error if not. If it's found to be
ready right away, it will pass through without error.


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.