对不起,标题可能混淆了。例如,eth1 替代 br100。所以eth1的ipv4地址为NULL。
/etc/网络/接口
br100 Link encap:Ethernet HWaddr 00:0c:29:75:e2:0a
inet addr:192.168.11.249 Bcast:192.168.11.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe75:e20a/64 Scope:Link
eth1 Link encap:Ethernet HWaddr 00:0c:29:75:e2:0a
inet6 addr: fe80::20c:29ff:fe75:e20a/64 Scope:Link
NULL
当我获得 eth1 的 ipv4 时,如何读取值?
这是我的getipv4
功能:
sIPv4 = new string[giIfaceCount];
struct ifaddrs *ifAddrStruct;
struct ifaddrs *ifa;
void *tmpAddrPtr;
if (getifaddrs(&ifAddrStruct) == -1)
{
perror("getifaddrs");
exit(1);
}
int iFlag4 = 0;
for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next)
{
if (ifa->ifa_addr->sa_family == AF_INET)
{
tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
char addressBuffer[1024];
if (strcmp(inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, 1024),"NULL") == 0)
{
cout << "addressBuffer = NULL" << endl;
}
else
{
inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, 1024);
if (strcmp(ifa->ifa_name,"lo") == 0)
{
continue;
}
else
{
*(sIPv4+iFlag4) = addressBuffer;
testIPv4.append(addressBuffer);
testIPv4.append(" ");
iFlag4 = iFlag4+1;
}
}
}
}