How can I make my program read the IP of the computer it's on?
Thanks, August.
How can I make my program read the IP of the computer it's on?
Thanks, August.
You can't do it FROM the computer, technically. But, you can make it download the page from www.whatismyip.org and then read the file.
i think its possible to get it right off the pc itself. however the code for it is OS specific.
i don't know much about the windows api but i've seen the code before. you would include a windows library, i think its winsock.h or something like that.
or, platform specific, use the system to call ipconfig to get the ip of active ethernet cards on windows
use ifconfig system call on *x systems ( macosx, linux, irix, openbsd, freebsd, netbsd, unix, bsd , aix, hpux )
Originally Posted by Jeff Henager
It depends which IP you want, and whether you're on a router or not. If you want your computer's local IP (e.g. The IP your router gave you) then that's one method, but if you want your internet IP (the one the rest of the world knows your modem by) that's a different method from the previous.Originally Posted by Cool-August
Explain which one you need/would like, and it'll be easier to guide you in the correct direction.
Perhaps gethostname(..)
Internet IP is what I want.
char* cars;
gethostname(cars,30);
MessageBox(0,cars,0,0);
// Is not working right!
You can't get the computer's internet IP from your computer, I'm telling you.
Will this help?
BobCode:#include <winsock.h> #include <stdio.h> int main(void) { WSAData wsaData; struct in_addr addr; char *IPaddress; char hn[80] = {0}; struct hostent *phostentry; WSAStartup(MAKEWORD(1, 1), &wsaData); if (gethostname(hn, sizeof(hn)) == SOCKET_ERROR) { printf("HostName error\n"); return 1; } printf("Hostname is %s\n", hn ); phostentry = gethostbyname(hn); if (phostentry == 0) { printf("Error on host lookup\n"); return 1; } for (int i = 0; phostentry->h_addr_list[i] != 0; ++i) { memcpy(&addr, phostentry->h_addr_list[i], sizeof(struct in_addr)); IPaddress = inet_ntoa(addr); printf("address %d: %s\n",i,IPaddress); } WSACleanup(); return 0; }
I dont know why, but you don't see it in ipconfig and that's the whole reasons sites like www.whatismyip.com exist. It's not like they'd hide it on the ipconfig menu.
But I guess it's possible, because IE and other browsers send the ip to that server first...
Hey people everyhting is possible!
And you can even get your ip form ur own pc!
To get your IP just enter the dos prompt or command prompt and type: ipconfig
You will get all that you want!
Regards!
well if u do ipconfig and your on a router ure gonna get the ip the router gives u.. at least thats wat i get and im assuming thats the way it is. even with netstat im not getting the matching ip that www.whatismyip.com gives me . hmm
C++ Rules!!!!
------------
Microsoft Visual Studio .NET Enterprise
Your computer doesn't send the info to the websites, the website finds it out itself.
Just download www.whatismyip.org in the background to a file using some function, and then read it in.