1

我正在做一个 ios 应用程序,它启动一个服务器并监听传入的连接,运行该应用程序的设备可能位于路由器后面,所以我需要进行端口转发。我正在尝试使用 DNSServiceNATPortMappingCreate 进行端口转发,但它总是返回错误代码 -65540

DNSServiceRef *sdRef = NULL ;
void ( *DNSServiceNATPortMappingReply) (DNSServiceRef sdRef,
                                        DNSServiceFlags flags,
                                        uint32_t interfaceIndex,
                                        DNSServiceErrorType errorCode,
                                        uint32_t externalAddress,
                                        DNSServiceProtocol protocol,
                                        uint16_t internalPort,
                                        uint16_t externalPort,
                                        uint32_t ttl,
                                        void *context );

DNSServiceNATPortMappingReply = &DNSServiceNATPortMappingCreate_callback ;

DNSServiceErrorType error = DNSServiceNATPortMappingCreate(sdRef,
                                                           0,
                                                           0,
                                                           kDNSServiceProtocol_TCP,
                                                           htons(2000),
                                                           htons(5000),
                                                           0,
                                                          DNSServiceNATPortMappingReply,
                                                           NULL
) ;

这是回调

void DNSServiceNATPortMappingCreate_callback(
                                         DNSServiceRef sdRef,
                                         DNSServiceFlags flags,
                                         uint32_t interfaceIndex,
                                         DNSServiceErrorType errorCode,
                                         uint32_t externalAddress, 
                                         DNSServiceProtocol protocol,
                                         uint16_t internalPort, 
                                         uint16_t externalPort, 
                                         uint32_t ttl, 
                                         void *context )
{
    printf("in callback\n") ;
}
4

1 回答 1

2

根据 DNSServiceDiscovery 的文档错误代码 -65540 表示kDNSServiceErr_BadParam

的文档DNSServiceNATPortMappingCreate建议您必须为DNSServiceRef作为第一个参数传递的参数分配存储空间。即你需要改变

DNSServiceRef *sdRef = NULL ;
DNSServiceErrorType error = DNSServiceNATPortMappingCreate(sdRef, ...

DNSServiceRef sdRef;
DNSServiceErrorType error = DNSServiceNATPortMappingCreate(&sdRef, ...
于 2013-07-16T09:13:20.780 回答