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 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 am currently trying to read from an xml file which records the jobs
I am trying to read a Network Stream from a socket. I know that
I'm trying to read a line from an io in a non-blocking way. Unfortunately
I'm trying to read data from a.csv file to ouput it on a webpage
I'm trying to read data from SQL Server database using Perl and the DBI
I am trying to read data from an RS-232 port. Does anyone have an
I am trying to read from a csv file and insert the data into
I'm trying to read from a file, but the only thing I get on
I'm currently trying to run MSTest.exe from NCover, but I believe the question could
I'm trying to read bytes from the accessory's InputStream without blocking the thread (if

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.