This is the mail archive of the cygwin@sourceware.cygnus.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Finding Out My Own IP-Address


> It's been a while since I've done socket programming, but if I remember
> correctly, what you want to do is use "gethostname" to get youre host's
> real name, and then "gethostbyname" to look up the IP.  Note that if you
> have more than one interface, it becomes more complicated.  Probably irrelevant
> in the WinDoze world, though.

gethostname will return your local machine name.  gethostbyname will
return the address for a name given it.  If you do this, you will
just be looking up your local machine name in DNS.  If you have a
dynamic assigned address, DNS will most likely not know about this
(note: there are some ISPs who have written their own DNS to know
about dynamic IPs, but this is the exception, not the rule).

Using DNS is not the best way to get your local IP address.  It amounts
to asking another machine what your IP should be.  The other machine may
be right, but it may be wrong, or it may not even be there.

You can find out your IP address with local tools (as was pointed
out by a few previous posters).  Or, you can just make a connection
and ask what addresses are bound to the connection.

        connect(sock, &addr, sizeof addr);
        adlen = sizeof addr;
        getsockname(sock, &addr, &adlen);
        /* addr now contains your local IP and the bound port */
        printf("My address: %s\n", inet_ntoa(addr.sin_addr));

This will give you the IP address (and the port) of the connection.
If you have multiple interfaces in your computer, you will get the
address of the interface that the connection was made over (each
interface has its own IP address).

> | /`--_   Nicholas R LeRoy      | In a world without fences, Who needs Gates?|
> |      nick.leroy@norland.com   | #include <disclaimer.h>                    |

                                             Tim N.

-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]