Jump to content
  • Advertisement

Archived

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

zackriggle

Get localhost IP

This topic is 5731 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 do I determine: A.) If a user is connected to the internet (this is from their own computer) B.) Get the localhost IP if they are connected Please help and thank you.

Share this post


Link to post
Share on other sites
Advertisement
I'm trying to do the same thing (using linux though, not winsock), and I'm wondering why the following code:


int main()
{
char ac[80];
gethostname(ac,sizeof(ac));
cout << "Host name is " << ac << ndl;

struct hostent *phe = gethostbyname(ac);

if(phe==0)
return 1;

for(int i=0; phe->h_addr_list != 0; i++)
{
struct in_addr addr;
memcpy(&addr, phe->h_addr_list[i], sizeof(struct inaddr));
cout << "Address " << i << ": " << inet_ntoa(addr) << endl;
}

return 0;
}


wouold only print out

Host name is localhost.localdomain
Address 0: 127.0.0.1

I know I have another IP besides my loopback (I'm on the internet, with a 56k), so where is it?



"There is no dark side of the moon really,
As a matter of fact, its all dark."

[edited by - DarkHamster on December 8, 2002 3:40:34 PM]

Share this post


Link to post
Share on other sites
The best way, I''m afraid, is to connect to another host and ask it. The reason is that you might be behind a NAT or a firewall which is not directly connected to your PC but is changing your address as your traffic passes through it.

There''s plenty of websites out there which, given the right URL, will simply return the IP address the request came from. Have a look at how those DynDNS clients work...

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
On a LAN is easier. You can use gethostbyname, and pass in localhost. This will return a list of all the IP addresses that your computer has. You can then just ignore the 127.0.0.1 address it''ll return and use the other(s).

Remember, though, that you can still have firewalls and NATs within a LAN. For a home setup, though, that''s going to be unlikely.

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!