-
August 24th, 2003, 11:35 PM
#1
Any API function to get IP Address of Machine?
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?
Thanks & Regards,
Ahsan
-
August 24th, 2003, 11:52 PM
#2
hai
u try with the fallowing function
gethostname()
u try with the function may be it will solave your problem
bye
naresh
-
August 25th, 2003, 12:13 AM
#3
Yes, I know this function already. But this gives name of the machine, not the IP. Can you suggest something about getting IP address of machine?
Regards,
Ahsan
-
August 25th, 2003, 12:25 AM
#4
Take a look at this FAQ...it is for Windows, however, it uses standard functions and should give you at least the way of doing it...
-
August 25th, 2003, 12:44 AM
#5
Another solution is getnameinfo().
Kuphryn
-
August 25th, 2003, 01:09 AM
#6
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...
-
August 25th, 2003, 01:15 AM
#7
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.
-
August 25th, 2003, 01:19 PM
#8
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...
-
August 25th, 2003, 03:45 PM
#9
Incorrect.
getnameinfo() is one solution. Pass in an empty string to getaddrinfo().
Kuphryn
-
August 26th, 2003, 06:45 PM
#10
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.
[PS. for IP address questions try www.WhatIsMyIpAddress.com]
- VR
-
August 26th, 2003, 11:01 PM
#11
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|