我是靠谱客的博主 缓慢黑夜,这篇文章主要介绍InetNtop() 与InetPton() 的使用,现在分享给大家,希望可以做个参考。

windows 把Inet_ntop()和Inet_pton 进行了封装,就变成了InetNtop() 和InetPton(),没有再msdn 上找到使用的例子。自己操作了一下,给出再VS2017上使用的例子。这里只给出了Ipv4的使用例子,ipv6的后续给出

#include "pch.h"
#include <iostream>
#include <Ws2tcpip.h>  //InetNtop 和InetPton 声明的文件

#pragma comment(lib,"Ws2_32.lib")

using namespace std;

int main()
{
	//int inet_pton(int af, const char *src, void *dst);
	PCWSTR src = TEXT("192.168.100.16");
	WCHAR ip[16] = TEXT("0");  //初始化是个好习惯
	struct in_addr dst;

	int i = 0;
	i = InetPton(AF_INET, src, &dst);
	if (1 == i)
    {
	    cout << "addr is " << dst.S_un.S_addr << endl;
    }

    //const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt);
	if (InetNtop(AF_INET, &dst.S_un.S_addr, ip, 100) == NULL)
	{
		cout << "error" << endl;
		return -1;
	}
	wcout << ip << endl;	//宽字符记得用宽字符的方式进行输出
}

最后

以上就是缓慢黑夜最近收集整理的关于InetNtop() 与InetPton() 的使用的全部内容,更多相关InetNtop()内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(134)

评论列表共有 0 条评论

立即
投稿
返回
顶部