使用这个非常有帮助 tutorial ,我已经能够对 iOS 模拟器和 Maya 之间的套接字通信进行测试(video)。这在模拟器和本地主机上运行得很好。但是,当我通过同一无线网络在我的设备上测试它时,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
}
就像我说的,与本地主机一起工作就像一个魅力,但在无线网络上失败。
最佳答案
好的,通过挖掘其他人用于在 Maya 中创建 commandPort 的 python 脚本找到了我自己的答案。它实际上根本不是由于 iOS 代码。事实证明,在 Maya 中,我需要创建一个 commandPort,不仅要使用端口号,还要使用 ip 地址。我的错误是我假设 commandPort 知道本地 ip 地址或者不在乎。显然它在乎。
之前(简化代码):
cmds.commandPort (n=':6328', stp='python')
之后:
cmds.commandPort (n='192.168.2.7:6328', stp='python')
关于iOS 套接字 : communication with Maya,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15211061/