The method for obtaining IP addresses for the C ++ domain name is successful !, Domain Name ip Address

Source: Internet
Author: User

The method for obtaining IP addresses for the C ++ domain name is successful !, Domain Name ip Address

 

BOOL  GetIpByDomainName(char *szHost,char szIp[100][100],int *nCount){    WSADATA        wsaData;       char           szHostname[100];       HOSTENT   *pHostEnt;       int             nAdapter   =   0;       struct       sockaddr_in   sAddr;       if   (WSAStartup(0x0101,   &wsaData))       {           AfxMessageBox("WSAStartup   failed   %s/n",   WSAGetLastError());           return FALSE;       }    pHostEnt   =  gethostbyname(szHost);    if (pHostEnt)    {        while   (   pHostEnt->h_addr_list[nAdapter]   )           {               memcpy   (   &sAddr.sin_addr.s_addr,   pHostEnt->h_addr_list[nAdapter],   pHostEnt->h_length);               char  szBuffer[1024] = {0};            sprintf(szBuffer,"%s", inet_ntoa(sAddr.sin_addr));            strcpy(szIp[nAdapter],szBuffer);            OutputDebugString(szBuffer);            nAdapter++;           }           *nCount = nAdapter;    }    else    {        DWORD  dwError = GetLastError();        CString  csError;        csError.Format("%d",dwError);        OutputDebugString(csError);        OutputDebugString("gethostbyname failed");        *nCount = 0;    }    WSACleanup();    return TRUE;   }

 

 1     int            nCount =0  ; 2     char        szIp0[100][100]; 3     char        szDomain[256] ={0}; 4     char        szIp[2048] ={0}; 5     strcpy(szDomain,"www.baidu.com"); 6     GetIpByDomainName(szDomain, szIp0,&nCount); 7     int nK = 0; 8     for (nK = 0; nK < nCount; nK++) 9     {10         strcat(szIp,szIp0[nK]);11         strcat(szIp,"\r\n");12     }13     OutputDebugString(szIp)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.