View Full Version : [c++]New to cpp, Need to get IP
blazes
03-22-2007, 04:10 PM
How would I go about getting the IP address of the machine running the program with c++? As I said i'm new and only know the basics.
BradLee
03-22-2007, 05:19 PM
Well, a computer pretty much always has more than one local IP address (127.0.0.1, etc..)
If you are on a Windows machine you can use winsock to get a list of your local IP addresses.
Look here: http://tangentsoft.net/wskfaq/examples/ipaddr.html
Linux has something similar, I just don't remember what it is at the moment.
That code will work on Linux too, if you remove all the winsock crap.. Winsock is basically just BSD sockets with a few extra function calls required before and after use :)
blazes
03-23-2007, 02:41 PM
Trying that code, I just get a bunch of 'unidentified' functions and variables, but I can't find out what the problem is.
[Linker error] undefined reference to `WSAStartup@8'
[Linker error] undefined reference to `WSAGetLastError@0'
[Linker error] undefined reference to `gethostname@8'
[Linker error] undefined reference to `gethostbyname@4'
[Linker error] undefined reference to `inet_ntoa@4'
[Linker error] undefined reference to `WSACleanup@0'
BradLee
03-24-2007, 04:34 PM
The problem is you are missing the winsock libraries. You can either include these through settings or to be simpler just include it through a compiler directive like this:
For winsock - #pragma comment(lib,"ws_32.lib")
For winsock2 - #pragma comment(lib,"ws2_32.lib")
vBulletin® v3.8.4, Copyright ©2000-2010, Jelsoft Enterprises Ltd.