IP ADDRESS OF MACHINE

Edmund Green confusedmund@yahoo.co.uk
Thu Feb 28 03:55:00 GMT 2002


 > In short I am looking for a program like /bin/netstat --route only this program is to be
 > much much much simpler. It just needs to retrieve the TCP/IP Address 192.168.0.2 and display
 > it.

   Well unfortunately I don't think that you can do this so simply, because the machine itself doesn't have an IP 
address, it is the interfaces that have the IP addresses and you will be using different interfaces depending upon where 
you are connecting to (for example consider the common situation of a computer which is acting as a firewall / router 
between the internet and an home network, it will have one external IP address on the interface talking to the internet, 
a different IP address on the interface on the home network, and a third IP address (127.0.0.1) for talking to itself)

   If you open a socket connection to somewhere, _then_ you can ask what the IP address of the interface on your end of 
the socket was, but until you do that (or manually read through your routing tables and network card configurations) 
then you don't know which IP address you're interested in (for example in your case being surprised that you were given 
the localhost address)

   Alternativly going the get host by name route as a previous poster demonstrated will work, but you might have to try 
setting up your /etc/hosts file as:

127.0.0.1 localhost.localdomain localhost
192.168.0.2 mymachine.mydomain mymachine

   if you've also got your /etc/host.conf set up to use "hosts, bind", otherwise if you put the name of your machine on 
the 127.0.0.1 line then it will still (correctly) return the address 127.0.0.1, because that is the IP address you are 
interested in if you want to open a socket to yourself.

Edmund.

ps, is there a more appropriate newsgroup for this kind of question than gcc-help?


_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com




More information about the Gcc-help mailing list