How to get the IP address of my machine through a 'C' program

Discussion in 'C Programming' started by sunny, Feb 16, 2007.

  1. sunny

    sunny Guest

    Hai,


    Anybody can give me idea how to get the ip address of my machine
    through a cprogram with out using the socket programming. Is there any
    system call to get the ip address of my machine that will be used in
    the c program to display the IP address of my machine.


    Regards,
    Sunny
     
    sunny, Feb 16, 2007
    #1
    1. Advertising

  2. sunny

    Chris Dollin Guest

    sunny wrote:

    > Anybody can give me idea how to get the ip address of my machine
    > through a cprogram with out using the socket programming. Is there any
    > system call to get the ip address of my machine that will be used in
    > the c program to display the IP address of my machine.


    There's no such function in standard C. For portability I'd guess something
    POSIXy, so asking in comp.unix.programmer might help (even if you're
    /not/ on a Unix box).

    --
    Chris "electric hedgehog" Dollin
    "No-one here is exactly what he appears." G'kar, /Babylon 5/
     
    Chris Dollin, Feb 16, 2007
    #2
    1. Advertising

  3. In article <er3uv2$k99$>,
    Chris Dollin <> wrote:
    >sunny wrote:
    >
    >> Anybody can give me idea how to get the ip address of my machine
    >> through a cprogram with out using the socket programming. Is there any
    >> system call to get the ip address of my machine that will be used in
    >> the c program to display the IP address of my machine.

    >
    >There's no such function in standard C. For portability I'd guess something
    >POSIXy, so asking in comp.unix.programmer might help (even if you're
    >/not/ on a Unix box).


    IOW:

    Off topic. Not portable. Cant discuss it here. Blah, blah, blah.

    Useful clc-related links:

    http://en.wikipedia.org/wiki/Aspergers
    http://en.wikipedia.org/wiki/Clique
    http://en.wikipedia.org/wiki/C_programming_language
     
    Kenny McCormack, Feb 16, 2007
    #3
  4. In article <er3uv2$k99$>,
    Chris Dollin <> wrote:

    >> Anybody can give me idea how to get the ip address of my machine
    >> through a cprogram with out using the socket programming. Is there any
    >> system call to get the ip address of my machine that will be used in
    >> the c program to display the IP address of my machine.


    >There's no such function in standard C.


    And since a computer can easily have several IP addresses (or none),
    you might want to consider what exactly you want.

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, Feb 16, 2007
    #4
  5. sunny

    sunny Guest

    i need to write a c program to get the ip of my own machine without
    using the sockets in it.
     
    sunny, Feb 16, 2007
    #5
  6. sunny

    santosh Guest

    sunny wrote:
    > i need to write a c program to get the ip of my own machine without
    > using the sockets in it.


    Please quote the post you're replying to.

    The standrad C language has no facility to do what you want. You'll
    probably have to call a POSIX function like gethostname() or
    gethostid(). Please look it up in your system's documentation or take
    this question to whatever group that is more specific to your
    platform. For example, if you're on a UNIX, post to
    comp.unix.programmer.
     
    santosh, Feb 16, 2007
    #6
  7. In article <>,
    sunny <> wrote:

    >i need to write a c program to get the ip of my own machine without
    >using the sockets in it.


    But why? What should the program do if the machine hasn't got an IP
    address, or has several?

    And as you've been told, there's no way to do it in standard C; you'll
    need to ask in a group that deals with your particular operating
    system. There may be a function that returns all the IP addresses
    associated with your machine, or perhaps something that tells you the
    name (or one of the names) of your machine, and a function that lets
    you look up the IP address(es) assocument with that name.

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, Feb 16, 2007
    #7
  8. Richard Tobin wrote:

    > In article <er3uv2$k99$>,
    > Chris Dollin <> wrote:
    >
    >>> Anybody can give me idea how to get the ip address of my machine
    >>> through a cprogram with out using the socket programming. Is there any
    >>> system call to get the ip address of my machine that will be used in
    >>> the c program to display the IP address of my machine.

    >
    >>There's no such function in standard C.

    >
    > And since a computer can easily have several IP addresses (or none),
    > you might want to consider what exactly you want.
    >
    > -- Richard


    And amusingly on top of that, the procedure used to discover the assigned IPs
    to each interface on a host is neither POSIX compliant nor standard. About
    the most "portable" way is using ioctl's which are system specific and hoping
    that the system you're on supports the more "standard" ioctl's.

    It pretty much sucks but is documented in UNPv1 (Stevens).
     
    Christopher Layne, Feb 16, 2007
    #8
  9. In article <er465d$1m2u$>,
    Richard Tobin <> wrote:
    >In article <>,
    >sunny <> wrote:
    >
    >>i need to write a c program to get the ip of my own machine without
    >>using the sockets in it.

    >
    >But why? What should the program do if the machine hasn't got an IP
    >address, or has several?
    >
    >And as you've been told, there's no way to do it in standard C; you'll
    >need to ask in a group that deals with your particular operating
    >system. There may be a function that returns all the IP addresses
    >associated with your machine, or perhaps something that tells you the
    >name (or one of the names) of your machine, and a function that lets
    >you look up the IP address(es) assocument with that name.


    IOW:

    Off topic. Not portable. Cant discuss it here. Blah, blah, blah.

    Useful clc-related links:

    http://en.wikipedia.org/wiki/Aspergers
    http://en.wikipedia.org/wiki/Clique
    http://en.wikipedia.org/wiki/C_programming_language
     
    Kenny McCormack, Feb 16, 2007
    #9
  10. sunny wrote:
    >
    > i need to write a c program to get the ip of my own machine without
    > using the sockets in it.


    You can't do this in standard C. The closest you can do is:

    char *GetMyIP()
    {
    return "127.0.0.1";
    }

    However, I doubt this is sufficient for what you need/want.

    There are "standard" function calls, outside the scope of standard C,
    which you can use. Perhaps something like gethostid()? However, to
    get more details, you need to ask elsewhere. Perhaps this is topical
    in comp.unix.programmer?

    Of course, as has been pointed out, what happens if the machine has
    more than one IP address? What if it has none?

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, Feb 16, 2007
    #10
  11. Kenneth Brody wrote:
    > sunny wrote:
    >> i need to write a c program to get the ip of my own machine without
    >> using the sockets in it.

    >
    > You can't do this in standard C. The closest you can do is:
    >
    > char *GetMyIP()
    > {
    > return "127.0.0.1";
    > }
    >
    > However, I doubt this is sufficient for what you need/want.


    I'll add to the library:

    char *GetMyIPv6() {
    return "Not implemented";
    }

    Sorry. Couldn't resist.
     
    Clever Monkey, Feb 16, 2007
    #11
  12. In article <>,
    Kenneth Brody <> wrote:
    ....
    >Of course, as has been pointed out, what happens if the machine has
    >more than one IP address? What if it has none?


    More to the point, "machines" don't have IP addresses. NICs do.
    And, in many (most?) cases, the IP assigned to a given NIC isn't
    the IP address that you care about anyway (because of NAT).
     
    Kenny McCormack, Feb 16, 2007
    #12
  13. Clever Monkey wrote:
    >
    > Kenneth Brody wrote:
    > > sunny wrote:
    > >> i need to write a c program to get the ip of my own machine without
    > >> using the sockets in it.

    > >
    > > You can't do this in standard C. The closest you can do is:
    > >
    > > char *GetMyIP()
    > > {
    > > return "127.0.0.1";
    > > }
    > >
    > > However, I doubt this is sufficient for what you need/want.

    >
    > I'll add to the library:
    >
    > char *GetMyIPv6() {
    > return "Not implemented";
    > }
    >
    > Sorry. Couldn't resist.


    Shouldn't that be "return NULL;" to indicate an error? :)

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, Feb 16, 2007
    #13
  14. In article <er4rvv$e0h$>,
    Kenny McCormack <> wrote:

    >More to the point, "machines" don't have IP addresses. NICs do.


    NICs only need to read and write packets; everything else is up to
    the IP stack or is an optimization with the cooperation of the IP
    stack.
    --
    If you lie to the compiler, it will get its revenge. -- Henry Spencer
     
    Walter Roberson, Feb 16, 2007
    #14
  15. >Anybody can give me idea how to get the ip address of my machine
    >through a cprogram with out using the socket programming. Is there any
    >system call to get the ip address of my machine that will be used in
    >the c program to display the IP address of my machine.


    *THE* IP address? Isn't that a little like telling someone to press
    *THE* key on *THE* keyboard?

    Assuming it's got networking and TCP/IP, "127.0.0.1" is a surprisingly
    portable answer to this question.
     
    Gordon Burditt, Feb 17, 2007
    #15
  16. sunny

    Randy Howard Guest

    On Fri, 16 Feb 2007 05:16:18 -0600, sunny wrote
    (in article <>):

    > i need to write a c program to get the ip of my own machine without
    > using the sockets in it.
    >


    As others have indicated, and you have snipped away the context, this
    is tricky to do, and outside the scope of this particular newsgroup.
    You should ask in a newsgroup appropriate to your platform, and keep in
    mind that doing this correctly can be tricky, especially with systems
    with more than one network adapter.

    --
    Randy Howard (2reply remove FOOBAR)
    "The power of accurate observation is called cynicism by those
    who have not got it." - George Bernard Shaw
     
    Randy Howard, Feb 18, 2007
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil Hanna
    Replies:
    6
    Views:
    30,071
    Roedy Green
    Jul 5, 2003
  2. Replies:
    1
    Views:
    6,632
    Matt Humphrey
    Jun 3, 2006
  3. Abby
    Replies:
    1
    Views:
    500
    Jack Klein
    Aug 29, 2003
  4. ~neil~
    Replies:
    7
    Views:
    560
    George Fulk
    Sep 28, 2004
  5. Aries
    Replies:
    1
    Views:
    117
    Jeff Cochran
    Jan 22, 2004
Loading...

Share This Page