iOS模拟器可以连接到服务器,但iPhone不能

时间:2013-07-23 16:37:13

标签: ios serversocket

我正在构建一个相对简单的基于位置的应用程序,它使用套接字连接到处理位置更新的服务器。当我在模拟器上运行应用程序时,我没有问题连接到服务器,但当我在我的iPhone上运行它时无法连接。我在Macbook Pro上运行它,我的防火墙已关闭。有任何想法吗?这是应用程序中尝试连接和发送消息的代码:

 - (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];

}

谢谢!

1 个答案:

答案 0 :(得分:0)

我的猜测是您的网络服务器位于手机无法访问的内部网络上。