|
Main program
void GetHostInfoByAddr (const char * addr)
{
hostent * phost = NULL;
unsigned long u_ip = inet_addr (addr);
phost = gethostbyaddr ((char *)&u_ip, 4, AF_INET);
cout << WSAGetLastError () << endl;
if (phost == NULL)
{
cerr << "get host info failed ----" << GetLastError () << endl;
return;
}
cout << phost-> h_name << endl; // Error
return;
}
int main ()
{
WSADATA wsadata;
WSAStartup (MAKEWORD (2,2),&wsadata);
//GetHostInfoByAddr("202.102.14.141 ");
GetHostInfoByAddr ("218.30.66.201"); // Sohu IP, PING get
WSACleanup ();
system ("pause");
return 0;
}
WSAGetLastError () returns a value of 0 and phost is not empty, indicating that the function call was successful.
However, when outputting the host name, it reports "0xffff305c" memory referenced by the instruction "0x00421940. This memory cannot be" read ".
That is why ah?
If gethostbyaddr can only get the hostname of the LAN, but I can get the host correctly using 202.102.14.141, please verify my code, thank you. |
|