Hi,
Is there any API Function which can give us the IP Address of the machine on which the program is running. I require this for C/C++ on Unix plateform.
Can anyone help?
Originally posted by kuphryn
Another solution is getnameinfo().
Well...in order to use that you already need to know the IP address since it will be passed to 'getnameinfo()'....and he is basically trying to get the IP address...
Ciao, Andreas
"Software is like sex, it's better when it's free." - Linus Torvalds
There is no such function that retrieves IP address. However you can get it using several strokes of code (ioctl etc.). Simply download "ifconfig" source code and take it from it.
Originally posted by docent
There is no such function that retrieves IP address. However you can get it using several strokes of code (ioctl etc.). Simply download "ifconfig" source code and take it from it.
Well...that is not true....take a look at the previous replies...
Ciao, Andreas
"Software is like sex, it's better when it's free." - Linus Torvalds
This may not be helpful, as your question has already been answered (the answer being "no"), but you may wonder why there is no function to get the local IP address.
The reason is:
There is really no such thing as the local IP address.
Lets say you had a PC. Your PC has several PCI slots that you can plug various devices into. You could choose to plug in two ethernet cards, in which case you'd assign each one a different IP address, or they could each get different addresses from a DHCP server. They may even (most likely) be connected to two different networks, one having a local address and one having a public address. Whatever the case, there's no answer to "what's my IP Address".
So there is no concept of the IP address for a given machine.
However, if you create a socket and connect it to a remote host, you can use a sockets call to find the address on the local side of the socket. In other words, in order to make a connection, the TCP/IP stack has to choose an available network device on the appropriate network, through which to connect. If there's only one such device, it'll choose that one. Then you can find the address associated with the device it chose. This is not necessarily the address but it is the address being used for that connection. And if you're looking for the address at which the remote machine can reach you, this is your best guess. This isn't a guarantee though because things like firewalls and network address translators could stand in the way.
There are also other tricks that don't involve making a connection. I once wrote code that made up a random number and broadcast a packet on the local network with that number. When it received a packet with that number, it grabbed the address of the sender (itself!). I'm sure there are better tricks than this out there, but the problem hasn't come up again for me, so I haven't gone out and found them.
Originally posted by Andreas Masur
Well...that is not true....take a look at the previous replies...
Can't agree with you. I guess even that it is probably the only correct way (ioctl etc. as it is in ifconfig source code). Simple example 2 LAN cards with different IP addresses.
Bookmarks