Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Recv timeout on non-blocking socket

Posted on 2007-03-19
4
Medium Priority
?
7,918 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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

721 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