0

When I chnage to port number I'm connecting to to test the timout, my app freezes. I am calling [request setTimeoutInterval:10];, which I assume should be 10 seconds. But, the app hangs. Could it have something to do with it being a local server?

Code:

// call this when program first starts
-(void) nSendMessage : (NSString *) name  Password: (NSString *) password page: (NSString *) page
{

    // set the url
    NSString *address = @"http://localhost:1075/update";
    address=[ address stringByAppendingString: page];

    NSMutableURLRequest *request = 
    [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:address]];

    // post or get
    [request setHTTPMethod:@"POST"];

    // data to send
    NSString *postString = @"username=iusername&password=ipassword";
    NSString *sendString=[postString stringByReplacingOccurrencesOfString:@"iusername" withString: name];
    sendString=[sendString stringByReplacingOccurrencesOfString:@"ipassword" withString: password];

    [request setValue:[NSString 
                       stringWithFormat:@"%d", [sendString length]] 
   forHTTPHeaderField:@"Content-length"];

    [request setHTTPBody:[sendString 
                          dataUsingEncoding:NSUTF8StringEncoding]];

    [request setTimeoutInterval:10];
    [[NSURLConnection alloc] 
     initWithRequest:request delegate:self];  

//
//THE PROGRAM FREEZES HERE
//

    NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 

    NSString *response = [[NSString alloc] initWithData:urlData encoding:NSASCIIStringEncoding]; 

    // Phrase repl
    [self  nUpdateDisplay:response];

}
Anne
  • 26,765
  • 9
  • 65
  • 71
Ted pottel
  • 6,869
  • 21
  • 75
  • 134

1 Answers1

0

Your question looks like a dupe of this one:

iPhone SDK: URL request not timing out

So to summarise: the timeout lower limit is at least four minutes, even if you pass in a smaller value.

And as detailed at the above link, using the asynchronous method is usually the best option. If you use synchronous, as you are doing above, and you're in the main runloop, you will block your UI completely, which will cause the 'hang' effect.

Community
  • 1
  • 1
occulus
  • 16,959
  • 6
  • 53
  • 76