Get localhost IP
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]
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
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 only 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 on 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 on 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...
codeka.com - Just click 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."
"There is no dark side of the moon really,
As a matter of fact, its all dark."
On a LAN is easier. You can use
Remember, though, that you can still have firewalls and NATs within a LAN. For a home setup, though, that''s going to be unlikely.
codeka.com - Just click it.
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
Popular Topics
Advertisement
Recommended Tutorials
Advertisement