Sockets: Retrieving my IP

Started by
6 comments, last by Prozak 22 years, 4 months ago
Hi all, using Sockets (i''m reading Prima''s Multiplayer Game Programming) how does one retrieve his machine IP address? And what call should one make to know if the machine is connected to internet/intranet ? Thanks for tha help,

[Hugo Ferreira][Positronic Dreams][]

Advertisement
a) getsockname()
b) depends on your system.
"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
quote:Original post by pentium3id
And what call should one make to know if the machine is
connected to internet/intranet ?

Do you mean like is the modem connected? I think you can determine that, I think it''s buried in the TAPI for Windows.

If you mean, does this computer have an ip route to Internet? Then you have to do something underhanded, like ping a known internet server... or tracert to your website.


Magmai Kai Holmlor

"Oh, like you''ve never written buggy code" - Lee

"What I see is a system that _could do anything - but currently does nothing !" - Anonymous CEO
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Look into the IP Helper API. Specifically, the GetIpAddrTable() function to get your valid IP addresses, and GetIfTable to get the status for each given interface.
That will tell you if there''s a default route or not, but it won''t tell you if you actually have a route out onto the internet... (think LAN + Proxy)
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
ok, thanx all for tha help...

[Hugo Ferreira][Positronic Dreams][]

error..


[Hugo Ferreira][Positronic Dreams][]

"Research is what I''m doing when I don''t know what I''m doing. "

- Wernher Von Braun (1912-1977)



heres some code that gets your local ip address:

    #include <winsock.h>#include <iostream.h>main(){    hostent* localInfo = gethostbyname(NULL);  // get localhost name    in_addr addr;    memcpy(&addr,localInfo->h_addr_list[0],sizeof(in_addr));    cout << inet_ntoa(addr) << endl;}    


Edited by - barazor on December 30, 2001 12:46:01 AM

This topic is closed to new replies.

Advertisement