我正在构建一个相对简单的基于位置的应用程序,它使用套接字连接到处理位置更新的服务器。当我在模拟器上运行该应用程序时,连接到服务器没有问题,但是当我在我的 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];
}
谢谢!