使用c++写一个在linux系统下获取IPv6本地链路地址的程序
时间: 2024-05-30 15:16:24 浏览: 22
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <string.h>
#include <arpa/inet.h>
int main() {
int fd;
struct ifreq ifr;
struct sockaddr_in6 *addr;
char interface[] = "eth0"; // 以eth0为例
char ipv6[INET6_ADDRSTRLEN];
fd = socket(AF_INET6, SOCK_DGRAM, 0);
strncpy(ifr.ifr_name, interface, IFNAMSIZ);
if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
addr = (struct sockaddr_in6 *)&ifr.ifr_addr;
inet_ntop(AF_INET6, &addr->sin6_addr, ipv6, sizeof(ipv6));
printf("IPv6 address on %s: %s\n", interface, ipv6);
}
else {
printf("Failed to get IPv6 address on %s\n", interface);
}
close(fd);
return 0;
}
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)