0
我正在構建一個相對簡單的基於位置的應用程序,它使用套接字連接到處理位置更新的服務器。當我在模擬器上運行應用程序時,我沒有問題連接到服務器,但是當我在我的iphone上運行它時,它無法連接。我正在Macbook Pro上運行此功能,並且防火牆已關閉。有任何想法嗎?以下是試圖連接併發送消息的應用程序中的代碼:iOS模擬器可以連接到服務器,但iPhone不能
- (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];
}
謝謝!
該服務器上是否有其他服務,如Web服務器?如果是這樣,你可以連接到通過你的iPhone(如使用移動Safari)? –