如何将函数的返回inet_ntop
(const char *) 转换为string
?
编码:
string ip = inet_ntop(b->ifa_addr->sa_family,tempAddrPtr,addressOutputBuffer, sizeof(addressOutputBuffer));
总是导致段错误...
struct ifaddrs *a;
void *tempAddrPtr = NULL;
struct ifaddrs *b;
char addressOutputBuffer[INET6_ADDRSTRLEN];
int i=0;
i=getifaddrs(&a);
cout << i;
b=a;
cout << name;
for (b = a; b; b = b->ifa_next) {
cout << b->ifa_name;
if(b->ifa_addr->sa_family == AF_INET)
tempAddrPtr = &((struct sockaddr_in *)b->ifa_addr)->sin_addr;
else
tempAddrPtr = &((struct sockaddr_in6 *)b->ifa_addr)->sin6_addr;
printf(" Internet Address: %s \n",
inet_ntop(b->ifa_addr->sa_family,
tempAddrPtr,
addressOutputBuffer,
sizeof(addressOutputBuffer)));
string ip = inet_ntop(b->ifa_addr->sa_family,tempAddrPtr,addressOutputBuffer, sizeof(addressOutputBuffer));
}
freeifaddrs(a);