我正在做一个 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") ;
}