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
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.
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)
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;
}
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;
}