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 8637467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:29:19+00:00 2026-06-12T10:29:19+00:00

I have a server that sends data to a client every 5 seconds. I

  • 0

I have a server that sends data to a client every 5 seconds. I want the client to block on read() until the server sends some data and then print it. I know read () is blocking by default. My problem is that my client is not blocking on read(). This is very odd and this does not seem to be a normal issue.

My code prints “Nothing came back” in an infinite loop. I am on a linux machine, programming in c. My code snippet is below. Please advice.

while(1)
{
    n = read(sockfd, recvline, MAXLINE);
    if ( n > 0) 
    {
        recvline[n] = 0;    
        if (fputs(recvline, stdout) == EOF)
            printf("fputs error");
    }
    else if(n == 0)
        printf("Nothing came back");
    else if (n < 0)
        printf("read error");
}
return; 
  • 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-06-12T10:29:21+00:00Added an answer on June 12, 2026 at 10:29 am

    There may be several cause and several exceptions are possible at different place:

    1. check socket where you create:

      sockfd=socket(AF_INET,SOCK_STREAM,0);  
      if (sockfd==-1) {
          perror("Create socket");
      }
      
    2. You and also enable blocking mode explicitly before use it:

      // Set the socket I/O mode: In this case FIONBIO  
      // enables or disables the blocking mode for the   
      // socket based on the numerical value of iMode.  
      // If iMode = 0, blocking is enabled;   
      // If iMode != 0, non-blocking mode is enabled.
      ioctl(sockfd, FIONBIO, &iMode);  
      

      or you can use setsockopt as below:

       struct timeval t;    
       t.tv_sec = 0;
       tv_usec = 0;
       setsockopt(
            sockfd,     // Socket descriptor
            SOL_SOCKET, // To manipulate options at the sockets API level
            SO_RCVTIMEO,// Specify the receiving or sending timeouts 
            const void *(&t), // option values
            sizeof(t) 
        );   
      
    3. Check Read function call (Reason of bug)

      n = read(sockfd, recvline, MAXLINE);
      if(n < 0){  
          perror("Read Error:");
      }  
      
    4. Also check server code!:

      1. May your server send some blank(non-printable, null, enter) charter(s). And your are unaware of this. Bug you server code too.

      2. Or your server terminated before your client can read.

    5. One more interesting thing, Try to understand:

      When you call N write() at server its not necessary there should be N read() call at other side.

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

Sidebar

Related Questions

I have a C application that sends data to a UDP server every few
I have a TCP server that listens for an incoming client, then sends it
I have a Client program by tcp connection that send data to a server.
I have a client TCP socket that writes a few bytes every five seconds,
I have a Web server that updates its data once per minute, and want
I have a MIDlet that sends TCP messages over the network to a server
We have a client application that needs to send messages to a server for
I have a huge Web App that's switching from a HTML-rendered-on-the-server-and-pushed-to-the-client approach to a
I have created a simple chat server that is driven by client polling. Clients
Lets say I have a chat program that every time someone sends a message,

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.