Finally solved it.
I ended up abandoning -portForName:host:
and instead pass the port number via commandline to the child process.
The child process contacts the parent process this way:
int port = /* parent process' port number passed via command line */
NSSocketPort *port = [[NSSocketPort alloc] initRemoteWithTCPPort:port host:nil];
NSConnection* connection = [NSConnection connectionWithReceivePort:nil sendPort:port];
The child process gets the port number from the parent process via commandline. The parent process find its own port number via this category.
@implementation NSSocketPort (ObtainPortNumber)
-(int)portNumber {
NSSocketNativeHandle sock = [self socket];
NSData *address = self.address;
if ([address length] != sizeof(struct sockaddr_in)) {
NSLog(@"NSSocketPort (ObtainPortNumber) - Mismatch size of address vs size of sockaddr_in.");
return -1;
}
struct sockaddr_in addr = *((struct sockaddr_in*)[address bytes]);
socklen_t len = sizeof(addr);
if (getsockname(sock, (struct sockaddr *)&addr, &len) == -1) {
NSLog(@"NSSocketPort (ObtainPortNumber) - getsockname failed.");
return -1;
}
int portNumber = ntohs(addr.sin_port);
return portNumber;
}
@end