最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++中inet_pton、inet_ntop函數(shù)的用法

 更新時(shí)間:2023年08月24日 14:28:10   作者:Lei Gong  
這篇文章主要介紹了C++中inet_pton、inet_ntop函數(shù)的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

C++中inet_pton、inet_ntop函數(shù)

頭文件

windows下:

#include <WS2tcpip.h>

linux下:

#include <sys/socket.h>
#include <netinet/in.h>
#include<arpa/inet.h>

inet_pton函數(shù)

將點(diǎn)分十進(jìn)制串轉(zhuǎn)換成網(wǎng)絡(luò)字節(jié)序二進(jìn)制值,此函數(shù)對(duì)IPv4地址和IPv6地址都能處理。

  • 第一個(gè)參數(shù)可以是AF_INET或AF_INET6:
  • 第二個(gè)參數(shù)是一個(gè)指向點(diǎn)分十進(jìn)制串的指針:
  • 第三個(gè)參數(shù)是一個(gè)指向轉(zhuǎn)換后的網(wǎng)絡(luò)字節(jié)序的二進(jìn)制值的指針。

inet_ntop函數(shù)

和inet_pton函數(shù)正好相反,inet_ntop函數(shù)是將網(wǎng)絡(luò)字節(jié)序二進(jìn)制值轉(zhuǎn)換成點(diǎn)分十進(jìn)制串。

  • 第一個(gè)參數(shù)可以是AF_INET或AF_INET6:
  • 第二個(gè)參數(shù)是一個(gè)指向網(wǎng)絡(luò)字節(jié)序的二進(jìn)制值的指針;
  • 第三個(gè)參數(shù)是一個(gè)指向轉(zhuǎn)換后的點(diǎn)分十進(jìn)制串的指針;
  • 第四個(gè)參數(shù)是目標(biāo)的大小,以免函數(shù)溢出其調(diào)用者的緩沖區(qū)。

樣例:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main (void)
{
	char IPdotdec[20]; //存放點(diǎn)分十進(jìn)制IP地址
	struct in_addr s; // IPv4地址結(jié)構(gòu)體
	// 輸入IP地址
	printf("Please input IP address: ");
	scanf("%s", IPdotdec);
	// 轉(zhuǎn)換
	inet_pton(AF_INET, IPdotdec, (void *)&s);
	printf("inet_pton: 0x%x\n", s.s_addr); // 注意得到的字節(jié)序
	// 反轉(zhuǎn)換
	inet_ntop(AF_INET, (void *)&s, IPdotdec, 16);
	printf("inet_ntop: %s\n", IPdotdec);
}

inet_pton和inet_ntop使用總結(jié)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main (void)
{
	char IPdotdec[20]; //存放點(diǎn)分十進(jìn)制IP地址
	struct in_addr s; // IPv4地址結(jié)構(gòu)體
	// 輸入IP地址
	printf("Please input IP address: ");
	scanf("%s", IPdotdec);
	// 轉(zhuǎn)換
	inet_pton(AF_INET, IPdotdec, (void *)&s);
	printf("inet_pton: 0x%x\n", s.s_addr); // 注意得到的字節(jié)序
	// 反轉(zhuǎn)換
	inet_ntop(AF_INET, (void *)&s, IPdotdec, 16);
	printf("inet_ntop: %s\n", IPdotdec);
	return 0;
}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

龙江县| 贵南县| 尉犁县| 临沧市| 突泉县| 台中县| 丰顺县| 兴海县| 伊春市| 井冈山市| 甘孜县| 英超| 闻喜县| 孝昌县| 甘谷县| 静海县| 绥化市| 北流市| 西峡县| 伊川县| 桑日县| 娱乐| 陆河县| 攀枝花市| 赫章县| 荆门市| 分宜县| 平谷区| 同德县| 仁怀市| 新乡市| 宜黄县| 梁山县| 乐山市| 泰州市| 漯河市| 阿尔山市| 马边| 来凤县| 屏边| 谷城县|