CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Mar 2003
    Posts
    71

    Question 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

  2. #2
    Join Date
    May 2003
    Location
    India
    Posts
    111
    hai

    u try with the fallowing function

    gethostname()

    u try with the function may be it will solave your problem

    bye
    naresh

  3. #3
    Join Date
    Mar 2003
    Posts
    71
    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

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    • 'gethostbyname()'


    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...

  5. #5
    Join Date
    Feb 2002
    Posts
    5,757
    Another solution is getnameinfo().

    Kuphryn

  6. #6
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    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...

  7. #7
    Join Date
    Aug 2003
    Posts
    3
    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.

  8. #8
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    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...

  9. #9
    Join Date
    Feb 2002
    Posts
    5,757
    Incorrect.

    getnameinfo() is one solution. Pass in an empty string to getaddrinfo().

    Kuphryn

  10. #10
    Join Date
    Nov 1999
    Location
    Los Angeles
    Posts
    38
    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

  11. #11
    Join Date
    Aug 2003
    Posts
    3
    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
  •  





Click Here to Expand Forum to Full Width

Featured