문제

두 개의 구성 요소, iPhone 구성 요소 및 Mac 구성 요소가있는 앱을 만드고 있습니다.그들은 Bonjour를 통해 서로 의사 소통을해야합니다.Mac 끝에서 다음 코드를 사용하여 서비스의 포트를 찾습니다.

NSSocketPort *socket = [[NSSocketPort alloc] init];
struct sockaddr *addr = (struct sockaddr *)[[socket address] bytes];
int port = 9876;
if(addr->sa_family == AF_INET) {
    port = ntohs(((struct sockaddr_in *)addr)->sin_port);
} else if(addr->sa_family == AF_INET6) {
    port = ntohs(((struct sockaddr_in6 *)addr)->sin6_port);
} else {
    [socket release];
    socket = nil;
    NSLog(@"The family is neither IPv4 nor IPv6. Can't handle!!!");
}
.

iPhone 끝 에서이 코드를 사용하고 시뮬레이터에서 앱을 실행하면 연결이 잘 작동합니다.그러나 실제 iPhone 에서이 코드를 실행하려고 시도했을 때 NSSocketPort가 iPhone에서 사용할 수 없음을 발견했습니다.포트 9876으로 서비스를 시작하려고하면 Mac App은 연결하려고 할 때 연결 거부 오류가 표시됩니다.그렇다면 nssocketport를 사용하지 않고 사용할 포트를 찾을 수 있습니까?

도움이 되었습니까?

해결책

OK Apple의 WITAP 코드를보고 포트를 얻는 방법을 작성하기 위해 약간 수정했습니다.

- (int) getPort {
CFSocketContext socketCtxt = {0, self, NULL, NULL, NULL};   

// Start by trying to do everything with IPv6.  This will work for both IPv4 and IPv6 clients 
// via the miracle of mapped IPv4 addresses.    

CFSocketRef witap_socket = CFSocketCreate(kCFAllocatorDefault, PF_INET6, SOCK_STREAM, IPPROTO_TCP, kCFSocketAcceptCallBack, nil, &socketCtxt);
uint32_t protocolFamily;

if (witap_socket != NULL)   // the socket was created successfully
{
    protocolFamily = PF_INET6;
} else // there was an error creating the IPv6 socket - could be running under iOS 3.x
{
    witap_socket = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_STREAM, IPPROTO_TCP, kCFSocketAcceptCallBack, nil, &socketCtxt);
    if (witap_socket != NULL)
    {
        protocolFamily = PF_INET;
    }
}

/*if (NULL == witap_socket) {
    if (error) *error = [[NSError alloc] initWithDomain:TCPServerErrorDomain code:kTCPServerNoSocketsAvailable userInfo:nil];
    if (witap_socket) CFRelease(witap_socket);
    witap_socket = NULL;
    return NO;
}*/


int yes = 1;
setsockopt(CFSocketGetNative(witap_socket), SOL_SOCKET, SO_REUSEADDR, (void *)&yes, sizeof(yes));

// set up the IP endpoint; use port 0, so the kernel will choose an arbitrary port for us, which will be advertised using Bonjour
if (protocolFamily == PF_INET6)
{
    struct sockaddr_in6 addr6;
    memset(&addr6, 0, sizeof(addr6));
    addr6.sin6_len = sizeof(addr6);
    addr6.sin6_family = AF_INET6;
    addr6.sin6_port = 0;
    addr6.sin6_flowinfo = 0;
    addr6.sin6_addr = in6addr_any;
    NSData *address6 = [NSData dataWithBytes:&addr6 length:sizeof(addr6)];

    CFSocketSetAddress(witap_socket, (CFDataRef)address6);
    /*if (kCFSocketSuccess != CFSocketSetAddress(witap_socket, (CFDataRef)address6)) {
        if (error) *error = [[NSError alloc] initWithDomain:TCPServerErrorDomain code:kTCPServerCouldNotBindToIPv6Address userInfo:nil];
        if (witap_socket) CFRelease(witap_socket);
        witap_socket = NULL;
        return NO;
    }*/

    // now that the binding was successful, we get the port number 
    // -- we will need it for the NSNetService
    NSData *addr = [(NSData *)CFSocketCopyAddress(witap_socket) autorelease];
    memcpy(&addr6, [addr bytes], [addr length]);
    return ntohs(addr6.sin6_port);

} else {
    struct sockaddr_in addr4;
    memset(&addr4, 0, sizeof(addr4));
    addr4.sin_len = sizeof(addr4);
    addr4.sin_family = AF_INET;
    addr4.sin_port = 0;
    addr4.sin_addr.s_addr = htonl(INADDR_ANY);
    NSData *address4 = [NSData dataWithBytes:&addr4 length:sizeof(addr4)];

    CFSocketSetAddress(witap_socket, (CFDataRef)address4);
    /*if (kCFSocketSuccess != CFSocketSetAddress(witap_socket, (CFDataRef)address4)) {
        if (error) *error = [[NSError alloc] initWithDomain:TCPServerErrorDomain code:kTCPServerCouldNotBindToIPv4Address userInfo:nil];
        if (witap_socket) CFRelease(witap_socket);
        witap_socket = NULL;
        return NO;
    }*/

    // now that the binding was successful, we get the port number 
    // -- we will need it for the NSNetService
    NSData *addr = [(NSData *)CFSocketCopyAddress(witap_socket) autorelease];
    memcpy(&addr4, [addr bytes], [addr length]);
    return ntohs(addr4.sin_port);
}

}
.

나는 그것이 아마도 최적이 아닐 것도 아마도 많은 오류를 꺼냈다.

다른 팁

iOS 7의 이름으로 NSNetService는 소켓을 만들고 바인딩 할 수 있습니다.포트 0으로 초기화하면 임의의 포트를 선택합니다.

NSNetService *service = [[NSNetService alloc] initWithDomain:@"local." type:@"_test._tcp." name:@"Test Service" port:0];
[service publishWithOptions:NSNetServiceListenForConnections];
.

연결이 설정되면 서비스가 델리게이트로 전송됩니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top