| |

VerySource

 Forgot password?
 Register
Search
View: 644|Reply: 3

Strange problem encountered when getting Sohu host with gethostbyaddr

[Copy link]

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-17 05:40:01
| Show all posts |Read mode
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.
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-1-21 21:36:01
| Show all posts
The Ping Sohu IP should be 221.236.12.205 or 221.236.12.214.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-17 13:30:01
| Show all posts
Please refer to the MSDN documentation. The return value of WSAGetLastError() is 0, which does not mean that the requested address must have a corresponding host name. If WSAGetLastError() == 11001, it means that there is no host name, so there is an access violation.

Please see msdn example code:


//----------------------
// Declare and initialize variables
hostent* remoteHost;
char* host_name;
unsigned int addr;

//----------------------
// User inputs name of host
printf("Input name of host: ");
host_name = (char*) malloc(sizeof(char*)*16);
fgets(host_name, 16, stdin);

// If the user input is an alpha name for the host, use gethostbyname()
// If not, get host by addr (assume IPv4)
if (isalpha(host_name[0])) {/* host address is a name */
  host_name[strlen(host_name)-1] = '\0'; /* NULL TERMINATED */
  remoteHost = gethostbyname(host_name);
}
else {
  addr = inet_addr(host_name);
  remoteHost = gethostbyaddr((char *)&addr, 4, AF_INET);
}

if (WSAGetLastError() != 0) {
  if (WSAGetLastError() == 11001)
  printf("Host not found...\nExiting.\n");
}
else
  printf("error#:%ld\n", WSAGetLastError());

// The remoteHost structure can now be used to
// access information about the host
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-8-17 16:45:01
| Show all posts
3Ks
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list