Solved

Recv timeout on non-blocking socket

Posted on 2007-03-19
4
7,906 Views
Last Modified: 2008-01-09
I'm trying to figure out how to avoid recv() calls that hang forever by using using a non-blocking socket along with the select() function.  This seems to be the standard way to avoid recv() calls that hang forever.

I'm having difficulty getting this to work however.  It seems to work sometimes, but occasionally the recv() call hangs.  Additionally, I think the loop I'm using is not doing something right, because it slows down the system indicating it is using a lot of CPU cycles.

Here is the recv() function I used:

      bool recv_non_block(char* buf)
      {      
            int status;
            do {
                  if ((status = recv(m_sock, buf, RECV_BUF_SIZE, 0)) == -1) {
                        if (errno == EWOULDBLOCK || errno == EAGAIN) {
                              if (Select()) continue;
                        }
                        else return false;
                  }
            } while (status != 0);
            return true;
      }

Note: The Select() function is simply a convenient wrapper for the select() function call.

Is the above loop a correct way to receive on a non-blocking socket?  Note also that since this can be used on an Internet website, I do not necessarily know the size of the data I am receiving, and therefore I need to rely on recv() returning 0 in order to end the loop.
0
Comment
Question by:chsalvia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 5

Expert Comment

by:ethan_mmg
ID: 18755946
Why not use recv in its own thread? It can block there while waiting for input. When data arrives, it can send a message to your main thread.
0
 

Author Comment

by:chsalvia
ID: 18758340
That's one possible solution.  But in a multi-threaded environment that would require twice as many threads.

I have discovered that creating a truly robust recv timeout is actually one of the most difficult things to program.  Every approach I try has some flaw in it.  For example, I initially tried using blocking sockets, and setting the SO_RCVTIMEO property.  This seemed to work well, but when I used a lot of concurrent threads, various recv calls would still mysteriously hang.  

Another option was to simply use SIGALRM to time out.  But this doesn't work well in a multi-threaded environment either.

So finally I tried non-blocking sockets.  This way I have more control over the recv() call.  Again, this seems to work fine initially.  But when there are a lot of concurrent threads, many of them still hang on recv().
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 18955637
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question