View Full Version : [c++]New to cpp, Need to get IP
blazes
March 22nd, 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
March 22nd, 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.
λ
March 23rd, 2007, 02:22 PM
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
March 23rd, 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
March 24th, 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")
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.