使用这个非常有用的教程,我已经能够对 iOS 模拟器和 Maya 之间的套接字通信进行测试(视频)。这与模拟器和 localhost 配合得很好。但是,当我通过相同的无线网络在我的设备上对其进行测试时,iOS 应用程序只是挂起,与 Maya 没有连接(连接后,会显示一些“提示”消息)。
我特别是 iOS 编程和套接字编程的新手,但我想知道我是否遗漏了什么。这是我按下“连接”按钮调用的方法:
- (void) initNetworkCommunication {
//assign text inputs to variables
self.ipAddress = self.inputIPAddress.text;
self.portNumber = [self.inputPortNumber.text intValue];
//create streams and use variables to populate connection method
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)(self.ipAddress), self.portNumber, &readStream, &writeStream);
inputStream = (__bridge NSInputStream *)readStream;
outputStream = (__bridge NSOutputStream *)writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
//send initial message to Maya
NSString *initResponse = [NSString stringWithFormat:@"cmds.headsUpMessage('connected to iPhone')"];
NSData *initData = [[NSData alloc] initWithData:[initResponse dataUsingEncoding:NSASCIIStringEncoding]];
[outputStream write:[initData bytes] maxLength:[initData length]];
//need some error checking or timeout mechanism
}
就像我说的那样,它在 localhost 上就像一个魅力,但在无线网络上失败了。