Jump to content
  • Advertisement
Sign in to follow this  
floatingwoods

gethostbyaddr fails but ping works

This topic is 3415 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I have a computer (server) on a specific IP address (e.g. 218.146.71.21) and I am trying to connect to it with following code from another computer (client):
	addr=inet_addr("218.146.71.21");
	hp=gethostbyaddr((char*)&addr,sizeof(addr),AF_INET);

The function however returns NULL. However checking the IP (dynamically assigned) on the server tells me it is the right one (218.146.71.21). From the client I can ping the server with ping 218.146.71.21 (it replies). How come my gethostbyaddr fails? Yesterday, using the exact same code I didn't have any problems connecting to the server. What is wrong? It can only be the client computer that has a strange problem I guess. Any help is very appreciated. Thanks!

Share this post


Link to post
Share on other sites
Advertisement
I'm strongly convinced that "sizeof(addr)" is wrong.
Print to the screen what it returns.
Or try hardcoding that parameter into the number 13 and see if that way works

Edit: If you're in Windows, may be worth checking out what WSAGetLastError has to say.

Share this post


Link to post
Share on other sites
How about you read the documentation?

Specifically, if the host is not "in the database" (whatever that locally means), you won't find it. For example, if it doesn't have reverse DNS, or if the DNS library you're using doesn't support reverse DNS look-up, you won't get a hostent back.

And if what you want to do is connect to a server, why would you call gethostbyaddr() at all? You already have a display string (the string you start with), and the in_addr (what you get back from inet_addr()) so the next step would be to call socket() and connect().

Share this post


Link to post
Share on other sites
Thanks to both of you for the replies.
Calling WSAGetLastError() returns WSANO_DATA. Not sure I understand what it means though! The documentation says:

"Valid name, no data record of requested type.
The requested name is valid and was found in the database, but it does not have the correct associated data being resolved for. The usual example for this is a host name-to-address translation attempt (using gethostbyname or WSAAsyncGetHostByName) which uses the DNS (Domain Name Server). An MX record is returned but no A record—indicating the host itself exists, but is not directly reachable."

Does that mean the problem lies on my server side (its internet connection)? But then, how come my ping works?
Is there anything I can do on the server side to avoid these problems in future? (until yesterday, and for the last week, I didn't have any such problems, using the same applications, the same server computer and the same internet connection.

Actually I am using an address of type ("xxx.dynalias.net") and I am using a dynamic IP updater. But since it didn't work anymore, I tried directly with the IP address (thinking it might be a problem with the IP updater), but that didn't change anything.

EDIT1: I also tried to set the sizeof() to 13 but that didn't help. The inet_addr function returns: 306615002

EDIT2: I tried this time not calling the gethostbyaddr like hplus0603 suggested, and I can connect to the server (using the IP address). The actual code I had is following (allowing to use a name or an IP as input:


if (inet_addr(connectionAddress.c_str())==INADDR_NONE)
hp=gethostbyname(connectionAddress.c_str());
else
{
addr=inet_addr(connectionAddress.c_str());
hp=gethostbyaddr((char*)&addr,sizeof(addr),AF_INET);
}
if(hp==NULL)
{
closesocket(conn);
std::cout << "gethost... failed.\n";
return(-2);
}
server.sin_addr.s_addr=*((unsigned long*)hp->h_addr);


This points to a problem with my IP address updater since the IP works but not the name. I made sure the updating software is running and displaying the correct data. It is. Where is the problem?

[Edited by - floatingwoods on May 20, 2009 12:44:14 AM]

Share this post


Link to post
Share on other sites
Thanks for helping me out with this problem! I actually found that my updater software is not operating correctly (starting the update but never finishing it).
Is it possible that a router can cause this type of trouble? Since yesterday I am using a router on the server side.. (but didn't have the problem yesterday)
Sorry about the trouble

[Edited by - floatingwoods on May 20, 2009 1:41:45 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!