我正在做一个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") ;
}
答案 0 :(得分:1)
根据DNSServiceDiscovery的文档,错误代码-65540表示kDNSServiceErr_BadParam
。
DNSServiceNATPortMappingCreate
的文档建议您必须为作为第一个参数传递的DNSServiceRef
分配存储空间。即你需要改变
DNSServiceRef *sdRef = NULL ;
DNSServiceErrorType error = DNSServiceNATPortMappingCreate(sdRef, ...
到
DNSServiceRef sdRef;
DNSServiceErrorType error = DNSServiceNATPortMappingCreate(&sdRef, ...