我获得了局域网上的某个机器名如何获得它的ip啊?
struct hostent *hp;
struct sockaddr_in server_addr;
hp=gethostbyname(名字);
if(hp==0)
{
return;
}
memcpy((char*)&server_addr.sin_addr,(char *)hp->h_addr,hp->hlength);
char szHostName[128];
if( gethostname(szHostName, 128) == 0 )
{
// Get host adresses
struct hostent * pHost;
int i;
pHost = gethostbyname(szHostName);
for( i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ )
{
CString str;
int j;
for( j = 0; j h_length; j++ )
{
CString addr;
if( j > 0 )
str += ".";
addr.Format("%u", (unsigned int)((unsigned
char*)pHost->h_addr_list[i])[j]);
str += addr;
}
// str now contains one local IP address - do whatever you want to do with it (probably add it to a list)
}
}
TOO ....
TCHAR szHostName[MAX_PATH];
if (gethostname(szHostName,MAX_PATH * sizeof(TCHAR)) != SOCKET_ERROR)
{
hostent *pHost;
pHost = gethostbyname(szHostName);
if (pHost)
{
m_strIP.Format("%d.%d.%d.%d",\
pHost->h_addr_list[0][0] & 0x00ff,\
pHost->h_addr_list[0][1] & 0x00ff,\
pHost->h_addr_list[0][2] & 0x00ff,\
pHost->h_addr_list[0][3] & 0x00ff);
}