对方的机器是ADSL拨号上网,IP动态获得。然后他们通过花生壳服务设置了一个域名来对应此动态IP。
怎么样通过这个域名获得其当前的IP呢?

解决方案 »

  1.   

    The gethostbyname function retrieves host information corresponding to a host name from a host database.struct hostent FAR *gethostbyname(
      const char FAR *name  
    );
    name 
    [in] Pointer to the null-terminated name of the host to resolve.
      

  2.   

    void DomainToIP(LPCTSTR sDomain,LPTSTR sIp)
    {
    HOSTENT* hst=NULL;
    struct in_addr ia;
    int iDomainLength = _tcslen(sDomain) + 1;
    char* lpsDomain = new char[iDomainLength];
    ZeroMemory(lpsDomain,iDomainLength);
    #ifdef _UNICODE
    WideCharToMultiByte(CP_ACP, 
    0,
    sDomain,
    -1,
    lpsDomain,
    iDomainLength,
    NULL,
    NULL);
    #else
    strcpy(sDomain,lpsDomain);
    #endif

    hst = gethostbyname(lpsDomain);
    if(hst != NULL)
    {
    memcpy(&ia.s_addr,hst->h_addr_list[0],sizeof(ia.s_addr));
    int iIpLength = strlen(inet_ntoa(ia)) + 1;
    char* lpcIp = new char[iIpLength];
    ZeroMemory(lpcIp,iIpLength);
    strcpy(lpcIp,inet_ntoa(ia));
    #ifdef _UNICODE
    MultiByteToWideChar(CP_ACP,0,lpcIp,iIpLength,sIp,iIpLength);
    #else
    strcpy(sIp,lpcIp);
    #endif
    delete[] lpcIp;
    // _stprintf(sIp,"%s",inet_ntoa(ia));
    }
    delete[] lpsDomain;
    }
      

  3.   

    在控制台,只需要ping就可以了,例如由下面可知道
    www.163.com的IP是202.108.9.16
    C:\Documents and Settings\ABC>ping www.163.comPinging www.cache.split.netease.com [202.108.9.16] with 32 bytes of data:Reply from 202.108.9.16: bytes=32 time=37ms TTL=55
    Reply from 202.108.9.16: bytes=32 time=38ms TTL=55
    Reply from 202.108.9.16: bytes=32 time=31ms TTL=55
    Reply from 202.108.9.16: bytes=32 time=32ms TTL=55Ping statistics for 202.108.9.16:
        Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
    Approximate round trip times in milli-seconds:
        Minimum = 31ms, Maximum =  38ms, Average =  34msC:\Documents and Settings\ABC>
      

  4.   

    用ping比较简单,把ping命令的返回值存到string里面然后在解析一下就可以了。可以用ShellExecute执行ping命令