2013-07-15 194 views
1

我正在做一個ios應用程序,它啓動一個服務器並偵聽傳入的連接,運行應用程序的設備可能位於路由器後面,所以我需要創建一個端口。 我試圖做一個端口轉發使用DNSServiceNATPortMappingCreate但它總是返回錯誤代碼-65540DNSServiceNATPortMappingCreate總是返回錯誤代碼-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 
) ; 

,這是基於對文檔進行DNSServiceDiscovery,錯誤代碼回調

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") ; 
} 

回答

1

- 65540表示kDNSServiceErr_BadParam

DNSServiceNATPortMappingCreate的文檔建議您必須爲作爲第一個參數傳遞的DNSServiceRef分配存儲空間。即你需要改變

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

DNSServiceRef sdRef; 
DNSServiceErrorType error = DNSServiceNATPortMappingCreate(&sdRef, ... 
+0

非常感謝,它的工作 –