• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
socket “read” hanging if the MacBook sleeps more than 10 minutes
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

socket “read” hanging if the MacBook sleeps more than 10 minutes


  • Subject: socket “read” hanging if the MacBook sleeps more than 10 minutes
  • From: Sabyasachi Ruj <email@hidden>
  • Date: Thu, 5 Nov 2009 19:39:06 +0530

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;
}
--
Sabyasachi
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: socket “read” hanging if the MacBook sleeps more than 10 minutes
      • From: Jens Alfke <email@hidden>
  • Prev by Date: Re: SCNetworkServiceCreate returns kSCStatusNoKey when trying to create a PPP service
  • Next by Date: Re: socket “read” hanging if the MacBook sleeps more than 10 minutes
  • Previous by thread: [solved] Re: Snow Leopard VPN connection - halfway there
  • Next by thread: Re: socket “read” hanging if the MacBook sleeps more than 10 minutes
  • Index(es):
    • Date
    • Thread