I'm building a relatively simply location-based app that connects using sockets to a server that processes the location updates. When I run the app on the simulator, I have no problem connecting to the server, but when I run it on my iphone it fails to connect. I'm running this on a Macbook Pro and my firewall is turned off. Any ideas? Here's the code in the app that attempts to connect and send a message:
- (void) sendToServer: (NSString *)message; {
NSLog(@"Trying to Connect.");
//Establish connection with server
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef) self.ip, self.port, &readStream, &writeStream);
self.inputStream = (__bridge NSInputStream *) readStream;
self.outputStream = (__bridge NSOutputStream *) writeStream;
[self.inputStream setDelegate: self];
[self.outputStream setDelegate: self];
[self.inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[self.outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[self.inputStream open];
[self.outputStream open];
//Send data to server
NSData *data = [[NSData alloc] initWithData:[message dataUsingEncoding:NSASCIIStringEncoding]];
[self.outputStream write:[data bytes] maxLength:[data length]];
[self.outputStream close];
[self.outputStream removeFromRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[self.inputStream close];
[self.inputStream removeFromRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
}
Thanks!
My guess is that your web server is on an internal network that your phone can't reach.