• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

C++在windows下获取本地主机ipv4地址和ipv6地址

OC/C/C++ 水墨上仙 1272次浏览

C++在windows下获取本地主机ipv4地址和ipv6地址

#include <Winsock2.h>
#include <stdio.h>
#include <iostream>
#include <cstring>
#include<ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib ")  //linking to the library
using namespace std;
int get_ip()
{
	struct addrinfo *ailist, *aip;        
	struct addrinfo hint;        
	struct sockaddr_in6 *sinp6;  
	PHOSTENT hostinfo;
	char hostname[255] = {0};      //主机名   
	char *port = "3294";                              //端口号 
	const char *addr;        
	int ilRc;
	gethostname(hostname, sizeof(hostname));
	if((hostinfo = gethostbyname(hostname)) == NULL)    //获得本地ipv4地址
	{
		errno = GetLastError();
		fprintf(stderr,"gethostbyname Error:%d\n", errno);
		return 1;
	}
	LPCSTR ip;
	while(*(hostinfo->h_addr_list) != NULL)        //输出ipv4地址
	{
		ip = inet_ntoa(*(struct in_addr *) *hostinfo->h_addr_list);
		printf("ipv4 addr = %s\n\n", ip);
		hostinfo->h_addr_list++;
	}
	hint.ai_family = AF_INET6;                   /*  hint 的限定设置  */
	hint.ai_socktype = SOCK_STREAM;     /*   这里可是设置 socket type    比如  SOCK——DGRAM */
	hint.ai_flags = AI_PASSIVE;                    // flags 的标志很多  。常用的有AI_CANONNAME;
	hint.ai_protocol = 0;                               /*  设置协议  一般为0,默认 */         
	hint.ai_addrlen = 0;                                /*  下面不可以设置,为0,或者为NULL  */
	hint.ai_canonname = NULL;        
	hint.ai_addr = NULL;        
	hint.ai_next = NULL;
	ilRc = getaddrinfo(hostname, port, &hint, &ailist);    /*通过主机名获得地址信息*/      
	if (ilRc < 0)        
	{               
		char str_error[100];                
		strcpy(str_error, (char *)gai_strerror(errno));                
		printf("str_error = %s", str_error);                
		return 0;        
	}
	if(ailist == NULL)
	{
		printf("sorry not find the IP address,please try again \n");
	}
	for (aip = ailist; aip != NULL; aip = aip->ai_next)                         /* 显示获取的信息  */
	{                		
		aip->ai_family == AF_INET6;
		sinp6 = (struct sockaddr_in6 *)aip->ai_addr;                                  /* 为什么是for 循环 ,先向下看 */
		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(){
	WORD wVersionRequested;
	WSADATA wsaData;
	int err;	
	wVersionRequested = MAKEWORD( 1, 1 ); 
	err = WSAStartup( wVersionRequested, &wsaData );//initiate the ws2_32.dll and match the version
	if ( err != 0 ) 
	{
		return 0;
	}
	if ( LOBYTE( wsaData.wVersion ) != 1 ||   //if the version is not matched ,then quit and terminate the ws3_32.dll 
		HIBYTE( wsaData.wVersion ) != 1 ) 
	{
		WSACleanup( );
		return 0; 
	}
	get_ip();
	return 0;
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明C++在windows下获取本地主机ipv4地址和ipv6地址
喜欢 (0)
加载中……