getaddrinfo 获取ipv6地址 linux,用getaddrinfo()函数,通过主机名获得ipv6和ipv4的地址...

111111111111111

using the function "getaddrinfo()" to get the IP address from the hostname you provided;

actually , the "getadrinfo()" function can get not only the IP address,but also more information about hostname,it's pretty useful,

especially for socket programing;

more information about the function ,you can go there :   http://www.cnblogs.com/cxz2009/archive/2010/11/19/1881693.html

Below is my program to parsing the IP address you wanted;and it's for both ipv4 and ipv6;

#include #include #include #include #include

#pragma     comment(lib, "ws2_32.lib ")  //linking to the library

using namespace std;

int main(){

WORD wVersionRequested;

WSADATA wsaData;

int err;

cout<

//******************//

the flowing text is to match the version of the library and to initialize the library, it's necessary for socket programing;

//*********************//

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;

}

//************************************************************************************************//

//copy

//************************************************************************************************//

while(1)

{

struct addrinfo *ailist, *aip;

struct addrinfo hint;

struct sockaddr_in6 *sinp6;

struct sockaddr_in *sinp4;

char hostname[255] = {0};      /*  这是用户名 */

char buf[INET_ADDRSTRLEN];

char *server = "3294";                              /*  这是服务端口号 */

const char *addr;

int ilRc;

cin>>hostname;

hint.ai_family = AF_UNSPEC;                   /*  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, server, &hint, &ailist);    /*通过主机名获得地址信息*/

if (ilRc < 0)

{

char str_error[100];

strcpy(str_error,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)                         /* 显示获取的信息  */

{

if(aip->ai_family == AF_INET)

{

sinp4 = (struct sockaddr_in *)aip->ai_addr;

addr = inet_ntoa( sinp4->sin_addr);

printf("ipv4 addr = %s\n", addr?addr:"unknow ");

}

else if(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");

}

}

// getaddrinfo();

while(1);

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值