什么是字节序?
字节序,顾名思义,指字节在内存中存储的顺序。比如一个int32_t类型的数值占用4个字节,这4个字节在内存中的排列顺序就是字节序。字节序有两种:
(1)小端字节序(Little endinan):按照内存增长的方向,数值低位存储在内存的低地址,高位存储在内存的高地址;
(2)大端字节序(Big endian):按照内存增长的方向,数值高位存储在内存的低地址,低位存储在内存的高地址。在网络传输中,是TCP/IP中规定好的一种数据表示格式,一般应用大端字节序,我们必须将主机字节序和网络字节序进行转换,否者无法通讯。
所以我们在实现网络编程的过程中必须要进行字节序的转化,在主机上把小端地址转化为大端地址。我们转换的配置的端口号和IP地址本质上就是把他们的存储内容和地址换了一下让网络能读懂我们传的是什么。
字节序转换API
uint16_t htons(uint16_t hostint16); //将主机字节序的端口 转换成 网络字节序的端口
uint16_t ntohs (uint16_t hostint16);//将网络字节序的端口 转换成 主机字节序的端口
int inet_aton(const char *strptr, void *sin_addr);//将点分十进制数串 转换成 32位网络字节序地址(大端格式)
uint32_t htonl(uint32_t hostint32);//将主机字节序的IP地址 转换成网络字节序的IP地址
int inet_ntoa(const char *strptr);将32位网络字节序IP 转换成 点分十进制数串
代码展示:API运用
#include <stdio.h>
#include <arpa/inet.h>
int main()
{
struct in_addr ipaddr;
unsigned long addr = inet_addr("192.168.1.100");
printf("addr = %u\n", ntohl(addr));
ipaddr.s_addr = addr;
printf("%s\n", inet_ntoa(ipaddr));
return 0;
}
所以是成功的.
代码展示:判断是否为大小端
#include<stdio.h>
#include<arpa/inet.h>
int main()
{
unsigned long a = 0x23456789;
unsigned char*p = (unsigned char*)&a;
printf("%0x %0x %0x %0x \n",*p,*(p+1),*(p+2),*(p+3));
unsigned long b = htonl(a);
unsigned char *f = (unsigned char*)&b;
printf("%0x %0x %0x %0x \n",*f,*(f+1),*(f+2),*(f+3));
return 0;
}
转化前我们是高数值放在低位,之后相反。所以我的主机是小端。链接: 套接字编程.