クロスプラットフォームのインテリジェントDNS

1、Linuxプラットフォーム

ボイド get_ips_by_name(文字列ドメイン、ベクトル< ストリング >&IPS)
{ 
    ips.clear()。
    私はsize_tの。

    構造体たhostent hostbuf。
    たhostent * PHOST =&hostbuf。
    チャー    hostBuf [ 1024 ]。
    size_tのbuffLen = はsizeof (hostBuf)。
    int型の    RET;
    int型    ERRCODE; 

    RET = gethostbyname_r(domain.c_str()、PHOST、hostBuf、buffLen、&PHOST、&ERRCODE)。
    もし0!= RET)
    { 
        リターン
    }
    
    { 
        場合(!PHOST)
        { 
            返します
        } 

        のための(i = 0 ;!= NULL PHOST && pHost-> h_addr_list [I] = NULL;!私++ 
        { 
            構造体 in_addr形式* PADDR =(in_addr形式*)(pHost-> h_addr_list [I])。
            チャー str_ip [ 20 ] = { 0 }。
            inet_ntop(AF_INET、(constの ボイド *)(&(pAddr-> s_addr))、str_ip、20 ); 
            ips.push_back(str_ip)。
        } 
    } 
}

 

2、iOSのプラットフォーム

ボイド get_ips_by_name(文字列ドメイン、ベクトル< ストリング >&IPS)
{ 
    ips.clear()。
    チャー    szIp [ 32 ]。
    構造体たhostent *ホスト= のgethostbyname(domain.c_str())。
    もし(ホスト)
    { 
        ためint型 I = 0 ;!ホスト= NULL && hosts-> h_addr_list [I] = NULL;!私++ 
        { 
            構造体 in_addr形式* PADDR =(in_addr形式*)(hosts-> h_addr_list [I])。
            チャー str_ip [ 20 ] = { 0 }。
            inet_ntop(AF_INET、(constの ボイド *)(&(pAddr-> s_addr))、str_ip、20 ); 
            ips.push_back(str_ip)。
        } 
    } 
}

 

3、Windowsプラットフォーム

ボイド get_ips_by_name(文字列ドメイン、ベクトル< ストリング >&IPS)
{ 
    HOSTENT * ホスト。
    チャー    szIp [ 32 ]。

    ホスト = のgethostbyname(domain.c_str())。
    もし(ホスト!= 0 
    { 
        チャー ** ppAddr = hosts-> h_addr_list。
        一方、(!* ppAddr = NULL)
        { 
            strcpyの(szIp、INET_NTOA( *(LPIN_ADDR)* (ppAddr)))。
            ips.push_back(szIp)。
            ppAddr ++ ; 
        }
    } 
}

 

おすすめ

転載: www.cnblogs.com/share-ideas/p/10969319.html