getting hostname or domain name from the ip address

Mar 26, 2012 at 12:46pm
Hi ,
I need a help regarding the socket programming i am doing .
I am trying to get the host or domain name from the ip address ..
e.g for the ip address 74.125.236.210 ... i should get www.google.com

I have tried twomethods for this .. like ..
1
2
3
4
5
WSADATA wsaData;
WSAStartup(MAKEWORD(2,0),&wsaData);
 
unsigned long ip     = inet_addr("74.125.236.210");
struct hostent *host = gethostbyaddr((const char FAR*)&ip,sizeof in_addr,0);






1
2
3
4
5
6
7
8
9
struct sockaddr_in sa;
	u_short port = 27015;
	sa.sin_family = AF_INET;
	sa.sin_addr.s_addr = inet_addr("74.125.236.210");
	sa.sin_port = htons(port);
	char host[1024];
	char service[20];
	getnameinfo( (struct sockaddr *) &sa, sizeof sa, host, sizeof host, service, sizeof service, 0);
	CString strTemp = host ; 



for all the two method i get
maa03s17-in-f18.1e100.net

instead of
www.google.com 


I am using winsock for this. ..
please help..thanks in advance ..


Last edited on Mar 26, 2012 at 12:46pm
Mar 26, 2012 at 3:56pm
please give me some suggestion ..please .. I am not finding any thing on it ..
Mar 26, 2012 at 4:55pm
Things are not always that easy on the internet.
servers are aliased, or redirected etc...
Big busy sites like www.google.com will be mirrored across several several servers to share the load - can you imagine how slooooow google would be if it ran on just one computer???

Here is what I mean -
C:\Windows\system32>ping -a www.google.com

Pinging www.l.google.com [173.194.66.103] with 32 bytes of data:
Reply from 173.194.66.103: bytes=32 time=41ms TTL=45
Reply from 173.194.66.103: bytes=32 time=33ms TTL=45
Reply from 173.194.66.103: bytes=32 time=33ms TTL=45
Reply from 173.194.66.103: bytes=32 time=32ms TTL=45

C:\Windows\system32>ping -a 173.194.66.103

Pinging we-in-f103.1e100.net [173.194.66.103] with 32 bytes of data:
Reply from 173.194.66.103: bytes=32 time=32ms TTL=45
Reply from 173.194.66.103: bytes=32 time=44ms TTL=45
Reply from 173.194.66.103: bytes=32 time=37ms TTL=45
Reply from 173.194.66.103: bytes=32 time=32ms TTL=45


But if you put 173.194.66.103 in your internet browser - you will see the Google
website.
Last edited on Mar 26, 2012 at 5:01pm
Mar 26, 2012 at 5:12pm
Thanks guestgulkan ... ...
but is there no way to get the url using the ip address ..or domain name using the ip address.
Mar 26, 2012 at 6:04pm
There is the
host->h_aliases[0] field ot the hostent structure - this aliases field is an array of pointers to alternative names for the site (So you will probably find www.google.com in the first one. (h_aliases[0])
so

cout << host->h_aliases[0]; //check the first alternative name.

In truth - the h_name of the hostent is the official name, and the aliases are just alternatives (if they are any)
Last edited on Mar 26, 2012 at 7:26pm
Mar 29, 2012 at 3:41pm
thanks guestgulkan ..
but when i put

1
2
3
 strTemp = host->h_aliases[0];
	  strTemp = host->h_aliases[1];
	  strTemp = host->h_aliases[2];


strTemp is empty always ..for the array 0 , 1 , 2 ..
is there any other way or can you tell me as why this array is empty .
Mar 30, 2012 at 6:07pm
can any one let me know the answer ... I am really stuck on this one.
thanks in advance ..
Mar 30, 2012 at 7:44pm
There is nothing to get stuck about.

The h_aliases is a pointer to a - null terminated array of char*-
Unfortunately there is no field in the hostent structure that tels you how
many aliases they are - it is possible for there to be no aliases - so you cannot
really do strTemp = host->h_aliases[2] directly.

You can loop around the h_aliases array until you reach the NULL pointer end marker. One possible loop style could be:

1
2
3
4
5
6
 for (int count = 0;  host->h_aliases[count] !=0; ++count )
{
       //do whatever you want with the alias string
     //in this case I'm just going to show on screen
      cout << host->h_aliases[count] << endl;
}

Last edited on Mar 30, 2012 at 8:02pm
Mar 31, 2012 at 3:20pm
executing the code you gave me .. still
1
2
3
4
5
6
 for (int count = 0;  host->h_aliases[count] !=0; ++count )
{
       //do whatever you want with the alias string
     //in this case I'm just going to show on screen
      cout << host->h_aliases[count] << endl;
}


still
host->haliases[count] is empty ..
Mar 31, 2012 at 9:50pm
maybe they aren't any aliases - a site does not have to have a set of alias names.
Last edited on Mar 31, 2012 at 9:52pm
Topic archived. No new replies allowed.