1

使用这个非常有用的教程,我已经能够对 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 上就像一个魅力,但在无线网络上失败了。

4

1 回答 1

1

好的,通过挖掘其他人的 python 脚本以在 Maya 中创建 commandPort 找到了我自己的答案。它实际上根本不是由于 iOS 代码。事实证明,在 Maya 中,我不仅需要使用端口号,还需要使用 ip 地址来创建 commandPort。我的错误是我假设 commandPort 知道本地 IP 地址或不在乎。显然它在乎。

之前(简化代码):

cmds.commandPort (n=':6328', stp='python')

后:

cmds.commandPort (n='192.168.2.7:6328', stp='python')
于 2013-03-05T05:48:54.180 回答