C++在windows下获取本地主机ipv4地址和ipv6地址的代码
把內容過程中經常用的內容段記錄起來,下面內容段是關于C++在windows下獲取本地主機ipv4地址和ipv6地址的內容,應該是對碼農有所用處。
#include <Winsock2.h> #include <stdio.h> #include #include #include<ws2tcpip.h> using namespace std; int get_ip() { struct addrinfo hint;
PHOSTENT hostinfo; int ilRc; gethostname(hostname, sizeof(hostname)); { errno = GetLastError(); fprintf(stderr,"gethostbyname Error:%dn", errno); return 1; } LPCSTR ip; { printf("ipv4 addr = %snn", ip); hostinfo->h_addr_list++; } hint.ai_canonname = NULL;
hint.ai_addr = NULL;
hint.ai_next = NULL; if (ilRc < 0)
{
char str_error[100];
printf("str_error = %s", str_error);
return 0;
} if(ailist == NULL) { printf("sorry not find the IP address,please try again n"); } { aip->ai_family == AF_INET6; int i; printf("ipv6 addr = "); for(i = 0; i < 16; i++) { if(((i-1)%2) && (i>0)) { printf(":"); } printf("%02x",sinp6->sin6_addr.u.Byte[i]);
} printf(" n"); printf(" n");
} while(1); } int main(){
}
轉載于:https://juejin.im/post/5cc010a3f265da036023b32e
總結
以上是生活随笔為你收集整理的C++在windows下获取本地主机ipv4地址和ipv6地址的代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: lol让1.5局是什么意思 艾欧尼亚VS
- 下一篇: php 导出excel 乱码怎么办