Subject: Re: Finding Out My Own IP-Address
From: [email-masked] (Tim Newsham)

9 May 1998 03:18:48 -0700
Message-ID: <[email-masked]>
References: <9805070846.ZM18698@dopey>
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
To: [email-masked] (Nicholas R LeRoy)
Cc: [email-masked], [email-masked]

> 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?|
> |      [email-masked]   | #include <disclaimer.h>                    |

                                             Tim N.

-
For help on using this list (especially unsubscribing), send a message to
"[email-masked]" with one line of text: "help".