Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

gethostbyname and inet_ntoa return 127.0.0.1 as IP Address??

Status
Not open for further replies.

Skute

Programmer
Jul 21, 2003
272
GB
Hi,

how can i get the non-local IP address with the following functions?

hostent* pHostEntity = NULL;
pHostEntity = gethostbyname(m_szHostName);
pIPAddress = inet_ntoa(*((in_addr *)pHostEntity->h_addr_list[0]));

On the windows box it returns a local address of 192.168.1.1 but the linux box returns 127.0.0.1??

I know they are both local, but the linux box should be 192.168.1.2

Also, is there a way of telling if the IP address is a local or external address (ie, for those connected via network + broadband or something).

thanks
 
gethostbyname() internally creates a DNS request and send it to the DNS server configured in your TCP/IP Settings. It doesnt matters if it is a local or a remote hostname that you are giving as an argument to the gethostbyname(). It will simply create a DNS reuest and will try to send it over. It might be implemented in the gethostbyname() or TCP/IP stack that it checks the name in the DNS request for the local names before sending it over to the DNS Server.
But there can be implementation(I really doubt, but some older implementation can be there) which doesnt check and just sends it over to the DNS Server.

What do you mean by local/remote IP addresses. If you mean the addresses that are not in your network, then you can easily see by comparing the network Address in the IP Address with your machine's Network Address. If they are not same, then it belongs to some remote machine.
IP Address is actually:- <Network Address><Machine Address in the Local Network>

 
i mean, if i have 2 connections, 1 network, 1 broadband

Network = 192.168... = local
Broadband = 208.241... = external

Thats kinda what i meant.

thanks
 
Ok..
Then you can always compare the received IP Address to see if it is in 192.168.X.X domain(your own network). If not then it is external IP address.

Because say form your prompt if you do
nslookup I am sure you will not get any IP from these two domains. Then also it will be an external IP.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top