HOSTENT *phEnt;
UCHAR uIP[4];
CString strIP;phEnt=GetHostByName("www.csdn.net");memcpy(uIP,phEnt->h_addr_list[0],4);;//format the ip address
strIP.Format("%d.%d.%d.%d",uIP[0],
uIP[1],uIP[2],uIP[3]);

解决方案 »

  1.   

    memcpy(uIP,phEnt->h_addr_list[0],4);這一行是什麽錯誤?運行到這裏就非法操作了?
      

  2.   

    DWORD GetIpAddress(  const char *const p_pchUrl )
    {
    DWORD dwIp= inet_addr( p_pchUrl ); 
    if( dwIp != INADDR_NONE ) 
    return dwIp; 
    hostent* pstHost;
    pstHost = gethostbyname( p_pchUrl );
    if( pstHost == NULL ) 
    OutputError();
    return *( reinterpret_cast< DWORD * >( pstHost->h_addr ) );
    }
      

  3.   

    我自己寫了一個,大家分享吧:
    void GetIPFromHostName(CString &strHost)
    {
    struct in_addr iaDest; // Internet address structure    iaDest.s_addr = inet_addr(strHost);
        if (iaDest.s_addr == INADDR_NONE)
    {
    WORD v;
    WSADATA wd;
    PHOSTENT hostinfo;
    v=MAKEWORD(2,0);
    WSAStartup(v,&wd);
    hostinfo=gethostbyname(strHost);
    strHost=inet_ntoa(*(struct in_addr *)*hostinfo->h_addr_list);
    }
    }
      

  4.   

    不会吧?
    memcpy(uIP,phEnt->h_addr_list[0],4);這一行是什麽錯誤?運行到這裏就非法操作了? 
    就是从phEnt->h_addr_list[0]这里拷贝4各字节到uIP里面呀!
      

  5.   

    http://www.vchelp.net/article/get_ip.htm
    void print_all_ip(void)
    {
      char szHostName[128];
      const char* pszAddr;
      struct hostent * pHost;
      int i,j; 
      if( gethostname(szHostName, 128) == 0 )
      {
        pHost = gethostbyname(szHostName); 
        for( i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ ) 
        {/*对每一个IP地址进行处理*/
          pszAddr=inet_ntoa (*(struct in_addr *)pHost->h_addr_list[i]);
          printf("%s\n",pszAddr);/*打印*/
        }
      }
    }