Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 169209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:35:22+00:00 2026-05-11T12:35:22+00:00

One of my projects on Linux uses blocking sockets. Things happen very serially so

  • 0

One of my projects on Linux uses blocking sockets. Things happen very serially so non-blocking would just make things more complicated. Anyway, I am finding that often a recv() call is returning -1 with errno set to EAGAIN.

The man page only really mentions this happening for non-blocking sockets, which makes sense. With non-blocking, the socket may or may not be available so you might need to try again.

What would cause it to happen for a blocking socket? Can I do anything to avoid it?

At the moment, my code to deal with it looks something like this (I have it throw an exception on error, but beyond that it is a very simple wrapper around recv()):

int ret; do {     ret = ::recv(socket, buf, len, flags | MSG_NOSIGNAL); } while(ret == -1 && errno == EAGAIN);   if(ret == -1) {     throw socket_error(strerror(errno)); } return ret; 

Is this even correct? The EAGAIN condition gets hit pretty often.

EDIT: some things which I’ve noticed which may be relevant.

  1. I do set a read timeout on the socket using setsockopts(), but it is set to 30 seconds. the EAGAIN‘s happen way more often than once every 30 secs. CORRECTION my debugging was flawed, EAGAIN‘s don’t happen as often as I thought they did. Perhaps it is the timeout triggering.

  2. For connecting, I want to be able to have connect timeout, so I temporarily set the socket to non-blocking. That code looks like this:

    int      error = 0; fd_set   rset; fd_set   wset; int      n; const SOCKET sock = m_Socket;  // set the socket as nonblocking IO const int flags = fcntl (sock, F_GETFL, 0); fcntl(sock, F_SETFL, flags | O_NONBLOCK);  errno = 0;  // we connect, but it will return soon n = ::connect(sock, addr, size_addr);  if(n < 0) {      if (errno != EINPROGRESS) {         return -1;     } } else if (n == 0) {     goto done; }  FD_ZERO(&rset); FD_ZERO(&wset); FD_SET(sock, &rset); FD_SET(sock, &wset);  struct timeval tval; tval.tv_sec = timeout; tval.tv_usec = 0;  // We 'select()' until connect() returns its result or timeout n = select(sock + 1, &rset, &wset, 0, timeout ? &tval : 0); if(n == 0) {         errno = ETIMEDOUT;     return -1; }  if (FD_ISSET(sock, &rset) || FD_ISSET(sock, &wset)) {     socklen_t len = sizeof(error);     if (getsockopt(SOL_SOCKET, SO_ERROR, &error, &len) < 0) {         return -1;     } } else {     return -1; }  done: // We change the socket options back to blocking IO if (fcntl(sock, F_SETFL, flags) == -1) {     return -1; } return 0; 

The idea is that I set it to non-blocking, attempt a connect and select on the socket so I can enforce a timeout. Both the set and restore fcntl() calls return successfully, so the socket should end up in blocking mode again when this function completes.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T12:35:23+00:00Added an answer on May 11, 2026 at 12:35 pm

    It’s possible that you have a nonzero receive timeout set on the socket (via setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,...)) as that would also cause recv to return EAGAIN

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The linux kernel (and various other projects including git) have very nice makefiles that
I'm a hapless Linux coder trying to get one of my projects working in
I am attempting to port one of my hobby project to linux. Preferrably to
After numerous projects one-way asynchronous message passing looks like the only way to write
I have a solution with 3 projects.One of UI (contains web pages) and one
I have the digdog MapKitDragAndDrop in one my projects to achieve drag and drop
I've got a solution with many projects One of these proejcts is a MyProject.Web
I'm working on a solution that has two projects: One ASP.NET web application, and
I have a Visual Studio C++ solution with 3 projects: one contains all classes
I've got a junk directory where I toss downloads, one-off projects, email drafts, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.