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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:23:08+00:00 2026-05-15T19:23:08+00:00

I am writing an app, where a socket is connecting to a host and

  • 0

I am writing an app, where a socket is connecting to a host and downloading a file.
The application runs in Mac.

Now, while the app is downloading, if I put the MacBook in sleep mode for more than 10 minutes, 60% of the time the app hangs when the computer wakes up.

The stack trace shows that, it has hanged in the “read” call. I am able to reproduce this with a sample program also. Below, I have pasted the code of the sample program and the stack where it is hanging. How to solve this hanging?

Also, this is not just TCP/IP waiting that will come out in few minutes. I have waited for more than 12 hours, it did not come out.

The stack trace: –

Call graph:
    2466 Thread_2507
      2466 start
        2466 read$UNIX2003
          2466 read$UNIX2003

The program :-

#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <sys/socket.h>
#include <unistd.h>

#define buflen 131072
unsigned int portno = 80;
char hostname[] = "192.168.1.9";

int main() 
{
    int sd = socket(AF_INET, SOCK_STREAM, 0);  /* init socket descriptor */    
    struct sockaddr_in sin;
    struct hostent *host = gethostbyname(hostname);
    char buf[buflen];
    int len;
    int ret;
    FILE *fp;
    int i;

    if(sd == -1){
        printf("Could not create client socket\n");
        return 1;
    }

    /*set keep alive*/
    int optval = 1;
 int optlen = sizeof(optval);
    ret = setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen);
    if(ret != 0){
        printf("could not set socket option.\n");
        return 1;
    }

    /*** PLACE DATA IN sockaddr_in struct ***/
    memcpy(&sin.sin_addr.s_addr, host->h_addr, host->h_length);
    sin.sin_family = AF_INET;
    sin.sin_port = htons(portno);

    /*** CONNECT SOCKET TO THE SERVICE DESCRIBED BY sockaddr_in struct ***/
    if (connect(sd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
        perror("connecting");
        return 1;
    }

    char *str = "GET /general-log.exe / HTTP/1.0\n\n";

    ret = write(sd, str, strlen(str));
    if(ret < 0){
        printf("error while writing\n");
        return 1;
    }


    fp = fopen("downloaded.file", "wb+");
    if(fp == NULL){
        printf("not able to open the file.\n");
        return 1;
    }

    i = 0;
    while ((len = read(sd, buf, buflen)) > 0) {        
        printf("%d\t%d\n", i++, len);
        fwrite(buf, len, 1, fp); //we should check for return
    }
    if(len < 0){
        printf("Error while reading\n");
    }

    fclose(fp);   
    close(sd);
    return 0;
}

Update apparently the SO_RCVTIMEOUT is solving the problem.

struct timeval tv;
tv.tv_sec=10;
tv.tv_usec=0;
setsockopt ( m_sock, SOL_SOCKET, SO_RCVTIMEO, (char *) &tv, sizeof ( tv ) );

Is it okay to use SO_RCVTIMEO?

  • 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-15T19:23:09+00:00Added an answer on May 15, 2026 at 7:23 pm

    I couldn’t solve it using blocking sockets. I had to change the IMAP library to non-blocking sockets.

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

Sidebar

Related Questions

I'm trying to allow multiple connections to a little Java server type app. It
When I debug an OSX application from XCode, I can communicate with the application's
again i have got a problem with socket programming in Android. My Problem is
I'm planning on developing an app for android that requires a back-end server to
I have a Windows Service that I am in the process of writing. I
I have a Java-app with an InputStream, which is copying data to an OutputStream.
I've been working on a application for iPad that use sockets to communicate with
I'm not sure if this is a known issue that I am running into,
I connect a ıp cam(jpeg), and show this image, and ı repeat continually, After
I have wrote a simple client that use TcpClient in dotnet to communicate. In

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.