Please suggest how to get the IP address of my system (logical address) using C++ and Linux. Thanks in advance
I think you can use int getifaddrs(struct ifaddrs **ifap);
to obtain this data. Read man 3 getifaddrs
for more info. It might look like:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <ifaddrs.h>
int main(void) {
struct ifaddrs *ifap, *cur;
getifaddrs(&ifap);
for(cur = ifap; cur != NULL; cur = cur->ifa_next) {
if(cur->ifa_addr->sa_family != AF_INET)
continue;
printf("address value: %s, name: %s\n",
cur->ifa_addr->sa_data,
cur->ifa_name);
}
return 0;
}
You ask for "the" IP address, but there is no such thing. Your system can have any number of IP addresses, even if you only have one NIC. For starters, you'll have at least 127.0.0.1 and at least one IP on your LAN. You need to figure out how to determine which interface you desire, and then use an appropriate library call (which I can't remember off the top of my head) to look up the IP for that interface.
"Logical Address" is not meaningful. You either want an interface address (the IP that hosts on the same local network as the machine see), or you want the publicly-facing Internet address (the IP address hosts will see when this machine connects to them). The IP addresses will only be the same if the machine is directly connected to the Internet, which often isn't true.
Second, Linux hosts can (and do) have multiple interfaces, so which interface is just as important. It might be routing related (in which case it depends on the destination), or it might use policy routing (which again: will depend on the actual traffic).
Third: Linux hosts may have multiple addresses. That is, the system administrator may bind multiple IP addresses to the interface, either by using sub-interfaces (e.g. ifconfig eth0:2 ...
) or by simply adding the secondary addresses (e.g. ip addr add ip dev eth0
).
That's why your best bet is to tell the user what you want to do, and ask the user to give you the right piece of information, or just try and make the connection, and rely on the system to do the Right Thing.
For the few cases where you actually need an IP address (for example, if you're implementing an FTP client), a specialized approach will be the correct approach (in the FTP client case: using the results of getsockname()
on the control channel). Knowing exactly why you think you need the IP address (and what information you have) will help get you a better answer.
Thx all of u good guys, here is one solution that i applied
system("ifconfig | grep \"inet addr\" | grep -v \"127.0.0.1\" | awk -F: '{print $2}' | awk '{print $1}' > yourip.txt");//gets the ip address of a machine
ifstream myReadFile;
myReadFile.open("yourip.txt");
char output[15];
if (myReadFile.is_open())
{
while (!myReadFile.eof())
{
myReadFile >> output;
}
}
myReadFile.close();
string ip = (string) output;
std::remove("yourip.txt");
use command to get IP, store it in text file, read the text file and delete it.