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)