【Linux网络编程】网络字节序和地址转换

00. 目录

01. 主机序到网络序转换函数

相关函数

#include <arpa/inet.h>
uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort);
uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(uint16_t netshort);

h表示主机序  
n表示网络序
s表示short类型
l表示long类型

htonl函数

uint32_t htonl(uint32_t hostint32);
功能:
	将 32 位主机字节序数据转换成网络字节序数据
参数:
	hostint32:需要转换的 32 位主机字节序数据,uint32_t 为 32 为无符号整型
返回值:
	成功:返回网络字节序的值

htons函数

uint16_t htons(uint16_t hostint16);
功能:
	将 16 位主机字节序数据转换成网络字节序数据
参数:
	hostint16:需要转换的 16 位主机字节序数据,uint16_t,unsigned short int
返回值:
	成功:返回网络字节序的值

测试程序:

#include <stdio.h>
#include <arpa/inet.h>
 
int main(int argc, char *argv[])
{
	int a = 0x01020304;
	short int b = 0x0102;
	
	printf("htonl(0x%08x) = 0x%08x\n", a, htonl(a));
	printf("htons(0x%04x) = 0x%04x\n", b, htons(b));
	
	return 0;
}

测试结果:

deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ gcc 1.c 
deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ ./a.out  
htonl(0x01020304) = 0x04030201
htons(0x0102) = 0x0201
deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ 

02. 网络序到主机序转换函数

ntohl函数

uint32_t ntohl(uint32_t netint32);
功能:
	将 32 位网络字节序数据转换成主机字节序数据
参数:
	netint32:待转换的 32 位网络字节序数据,uint32_t,unsigned int
返回值:
	成功:返回主机字节序的值

ntohs函数

uint16_t ntohs(uint16_t netint16);
功能:
	将 16 位网络字节序数据转换成主机字节序数据
参数:
	netint16:待转换的 16 位网络字节序数据,uint16_t,unsigned short int
返回值:
	成功:返回主机字节序的值

测试代码:

#include <stdio.h>
#include <arpa/inet.h>
 
int main(int argc, char *argv[])
{
	int a = 0x01020304;
	short int b = 0x0102;
	
	printf("ntohl(0x%08x) = 0x%08x\n", a, ntohl(a));
	printf("ntohs(0x%04x) = 0x%04x\n", b, ntohs(b));
	
	return 0;
}

测试结果:

deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ gcc 1.c 
deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ ./a.out  
ntohl(0x01020304) = 0x04030201
ntohs(0x0102) = 0x0201
deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ 

03. inet_pton函数

inet_pton函数

#include <arpa/inet.h>
int inet_pton(int family, const char *strptr, void *addrptr);
功能:
	将点分十进制数串转换成 32 位无符号整数
参数:
	family:协议族( AF_INET、AF_INET6、PF_PACKET 等 ),常用 AF_INET
	strptr:点分十进制数串
	addrptr:32 位无符号整数的地址
返回值:
	成功返回1
    失败返回其它

测试代码:

#include <stdio.h>
#include <arpa/inet.h>
int main()
{
	char ip_str[]="192.168.12.133";
	unsigned int ip_uint = 0;
	unsigned char *ip_p = NULL;
 
	inet_pton(AF_INET,ip_str,&ip_uint);
	printf("in_uint = %#x\n",ip_uint);
	
	ip_p = (char *)&ip_uint;
	printf("in_uint = %d,%d,%d,%d\n",*ip_p,*(ip_p+1),*(ip_p+2),*(ip_p+3));
 
	return 0;
}

测试结果:

deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ gcc 1.c 
deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ ./a.out  
in_uint = 0x850ca8c0
in_uint = 192,168,12,133
deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ 

04. inet_ntop函数

const char *inet_ntop( int family, const void *addrptr,  char *strptr, size_t len );
功能:
	将 32 位无符号整数转换成点分十进制格式的字符串
参数:
	family:协议族( AF_INET、AF_INET6、PF_PACKET 等 ),常用 AF_INET
	addrptr:32 位无符号整数
	strptr:点分十进制数串
	len:strptr 缓存区长度
    len 的宏定义
    #define INET_ADDRSTRLEN   16  // for ipv4
    #define INET6_ADDRSTRLEN  46  // for ipv6

返回值:
	成功:则返回字符串的首地址
	失败:返回 NULL

测试代码:

#include <stdio.h>
#include <arpa/inet.h>
int main()
{
	unsigned char ip[] = {192, 168, 12, 133};
	char ip_str[16] = "\0";
 
	inet_ntop(AF_INET,(unsigned int *)ip,ip_str,16);
	printf("ip_str = %s\n",ip_str);
 
	return 0;
}

测试结果:

deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ gcc 1.c 
deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ ./a.out  
ip_str = 192.168.12.133
deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ 

05. 附录

发布了639 篇原创文章 · 获赞 2326 · 访问量 75万+

猜你喜欢

转载自blog.csdn.net/dengjin20104042056/article/details/103016045