Advertisement

Get localhost IP

Started by December 07, 2002 08:38 AM
5 comments, last by zackriggle 22 years, 2 months ago
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.
A.) I know there was a WinNT API function, but I can't remember ...
B.) open a socket and call getsockname on it.

Edit - trying to fix that #[~{ signature.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | Free C++ IDE. ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Boost C++ Lib ]


[edited by - Fruny on December 7, 2002 9:56:33 AM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
what are you using for this?
winsock has a local ip method.
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, sizeof(struct inaddr));<br>cout << "Address " << i << ": " << inet_ntoa(addr) << endl;<br>}<br><br>return 0;<br>}<br>  </pre>   </i> <br><br>wouold &#111;nly print out<br><br>Host name is localhost.localdomain<br>Address 0: 127.0.0.1<br><br>I know I have another IP besides my loopback (I'm &#111;n the internet, with a 56k), so where is it?    <br><br><HTML><BR><BR>"There is no dark side of the moon really,<BR>As a matter of fact, its all dark."<br><br><SPAN CLASS=editedby>[edited by - DarkHamster &#111;n December 8, 2002 3:40:34 PM]</SPAN>    


"There is no dark side of the moon really,
As a matter of fact, its all dark."
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.




How would I look up my local IP on a LAN, not the internet?



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


"There is no dark side of the moon really,
As a matter of fact, its all dark."
Advertisement
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.




This topic is closed to new replies.

Advertisement