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

  • Home
  • SEARCH
  • 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 5845545
In Process

The Archive Base Latest Questions

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

I am trying to read from a blocking socket, but I am wondering that

  • 0

I am trying to read from a blocking socket, but I am wondering that read() returns -1, which I think means that there’s currently no data to read – I would expect that it blocks until it can read the amount of bytes.

I also tried ensuring that the socket is in blocking mode and that a high timeout is set using:

int setBlockingIO(int fd)
{
       int flags = fcntl(fd, F_GETFL);
       fcntl(fd, F_SETFL, flags & (~O_NONBLOCK));
       int nTimeout = 30000; // 30 seconds
       setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&nTimeout, sizeof(int));
}

But this hasn’t changed anything.

My question:

  • What do I have to do that read() will really block?
  • Are there some pitfalls which I might hit? (bug in my program?)

I know that there’s another question on this topic, but there I cannot find the answer for my question.

UPDATE

Without setting a timeout, read() also returns (subjectivly) immediately -1

UPDATE 2

errno is 107 (ENOTCONN, Transport endpoint is not connected).
But the client side has not closed the connection in the meanwhile (ensured by a long sleep() after write() )

  • 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. Editorial Team
    Editorial Team
    2026-05-22T12:27:01+00:00Added an answer on May 22, 2026 at 12:27 pm

    It means that either there was a time-out, or possibly a signal interrupted the read. You can use the results of errno in errno.h to see what the error is, and if you want the error in a human-readable formating, you can use either strerror() or perror() from string.h or stdlib.h

    Update: According to the POSIX specification, you should be passing a struct timeval (defined in sys/time.h) set to the desired number of seconds and microseconds before a timeout occurs to setsockopt when specifying the SO_RCVTIMEO flag rather than casting an int to a const char*. So even though your client may be mis-behaving and causing a different error right now, you could still end up with problems further down the line if you’re sending the wrong argument types to the function.

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

Sidebar

Related Questions

I'm trying to parse results from queries over HTTP that can return up to
Problem : Trying to read from some electronic scales using the comport via Java
I am trying to read from a txt file (book) and then add every
I am trying to read from a text file and tokenize the input. I
I am trying to read from Python the WM_COPYDATA message some applications (I'm trying
I am trying to read from /dev/random and /dev/urandom and would like to know
My Arduino is stuck in an infinite loop trying to read from a pin
I'm trying to read the access for files and directories in Windows using this
I'm building a Windows application that will go against a SQL 2008 database. Some
I am running Windows 7, and am not usually a developer in this setting,

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.