Archived

This topic is now archived and is closed to further replies.

obtaining IP address of current computer

This topic is 5569 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How would I obtain the dialup IP of the current computer? I want the one exposed to the Internet if I''m behind a NAT router, not the local network IP. gethostbyname returns my local IP (192.168.0.4), instead of the one assigned to my cable modem.

Share this post


Link to post
Share on other sites
gethostbyname is probably returning more than on IP. You''ll need to use a bit of heiristics to determine which one is your external address (i.e. 127.0.0.1, 10.0.* and 192.168.* are reserved for "internal" use, and could be discounted as the internet address).

The most reliable method is to connect to some external server which will tell you your IP address (for example you can set up a .php script to print the IP of the connecting client and then just put it on your webserver and connect to that)

Finally, as granat pointed out, do you really need to know your IP address?

If I had my way, I''d have all of you shot!


codeka.com - Just click it.

Share this post


Link to post
Share on other sites
Yes, I really need to know it.

I noticed something while trying to find a solution. If I am behind a router, would my computer list the cable modem IP as one of its own IPs? gethostbyname does not appear to return the external IP.

Anyway, I''ve found a solution that relies on the server telling myself my IP. Thanks!

Share this post


Link to post
Share on other sites
There is no way to find out (via API calls on the local machine) what your ''global'' IP address looks like if you''re running through any kind of NAT. The local TCP/IP stack just doesn''t have that information because it doesn''t use it.. It just routes packets to its defined gateway and the gateway/NAT is the one who translates the headers to use the other IP address (and back again on the return side).

As you''ve already been advised (and it seems this is working for you) you can have the server tell you what the IP looks like on the other end of the connection, which should be good enough for whatever it is you''re doing (though I can''t think of any cases where the client really needs this information).

Share this post


Link to post
Share on other sites
I have the same problem...
Here is a exmaple of a prog that need this IP,
let''s say you want to make a chat in a client-server form,
you want to be the server, but you don''t have a permanent
connexion and you are behind a router.
When you start the server, you want client to find your
machine, one of the solution i found was to put my ip on
a ftp server. So that when you launch client, it goes to the
ftp server, download your ip and connect to the server.
It worked til the day I bought this router...

(s)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If you want to run a server behind a NAT, the best and easiest thing to do is just let the user running the server input the IP address. Since they will have to set up the port forwarding on the router to let the server receive connections anyway, theres a pretty good chance they know the IP.

Share this post


Link to post
Share on other sites