C++通过域名获取IP地址的方法;调试通过!

BOOL  GetIpByDomainName(char *szHost,char szIp[][],int *nCount)
{
WSADATA wsaData;
char szHostname[];
HOSTENT *pHostEnt;
int nAdapter = ;
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[] = {}; 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 = ;
}
WSACleanup();
return TRUE; }
     int            nCount =  ;
char szIp0[][];
char szDomain[] ={};
char szIp[] ={};
strcpy(szDomain,"www.baidu.com");
GetIpByDomainName(szDomain, szIp0,&nCount);
int nK = ;
for (nK = ; nK < nCount; nK++)
{
strcat(szIp,szIp0[nK]);
strcat(szIp,"\r\n");
}
OutputDebugString(szIp)
上一篇:python面向对象之继承与派生


下一篇:apache开源项目--Synapse