【1】字节序
1. 字节序的概念
字节序是指不同类型的CPU主机,内存存储 多字节整数 序列方式。
小端字节序:低序字节存储在低地址。
大端字节序:低序字节存储在高地址。
注意:
-
读取数据都是从低地址往高地址读取的,读取后,通过大小端进行转换。
-
字符串没有大小端的区别,因为字符串是单个字符组成的。
简述大小端的概念,用联合体的方式判断操作系统是大端还是小端
#include <stdio.h> union { unsigned int a; char b; }t; int main(int argc, const char *argv[]) { //联合体的方式 t.a = 0x1; printf("%#x %#x\n", t.a, t.b); if(1 == t.b) { printf("this is little-endian\n"); } else { printf("this is big-endian\n"); } //指针的方式验证 unsigned int c =1 ; char* pc = (char*)&c; if(1 == *pc) { printf("this is little-endian\n"); } else { printf("this is big-endian\n"); } return 0; }
2. 本地字节序与网络字节序
主机字节序: (host byte order)HBO
网络字节序: (networkbyte order)NBO 网络字节序是大端字节序
不同机器的HBO不一样,这个和cpu的设计有关。
所以网络传输需要转换成网络字节序,使用统一的字节序,避免兼容性问题
3. 字节序的转换
1)htons htonl 主机字节序--->网络字节序
头文件: #include <arpa/inet.h> 原型: uint32_t htonl(uint32_t hostlong); uint16_t htons(uint16_t hostshort); 参数: uint16_t hostshort:16位主机字节序 uint32_t hostlong:32位的主机字节序; 返回值: 返回对应的网络字节序;
2)ntohs ntohl网络字节序--->主机字节序
头文件: #include <arpa/inet.h> 原型: uint32_t ntohl(uint32_t netlong); uint16_t ntohs(uint16_t netshort); 参数: uint16_t hostshort:16位网络字节序 uint32_t hostlong:32位的网络字节序; 返回值: 返回对应的本机字节序;
#include <stdio.h>
#include <arpa/inet.h>
int main(int argc, const char *argv[])
{
unsigned int a = 0x87654321;
unsigned int b = htonl(a);
printf("%#x\n", b); //0x21436587
unsigned short c = htons(a); //把a强行转换成2个字节,变成 0x4321
printf("%#x\n", c); //0x2143
//网络字节序---》本机字节序
printf("%#x\n", ntohl(b));
printf("%#x\n", ntohs(c));
return 0;
}
4. 结构体对齐
编译器会对结构体进行对齐,加速CPU的取值周期,由于数据对齐也是与平台相关的,不同主机如果使用不同对齐方式,会导致数据无法解析。所以网络传输结构体时候一定要取消结构体对齐;
#include <stdio.h>
typedef struct
{
char a;
int b;
short c;
}A;
typedef struct
{
char a;
int b;
short c;
}__attribute__((packed)) B;
int main(int argc, const char *argv[])
{
printf("%ld\n", sizeof(A)); //12
printf("%ld\n", sizeof(B)); //7
return 0;
}
【2】IP转换
1.点分十进制---》网络字节序
1)inet_aton
头文件:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
原型:
int inet_aton(const char *cp, struct in_addr *inp);
参数:
char *cp:指定要转换成网络字节序的点分十进制字符串;
struct in_addr *inp:存储转换后的网络字节序的IP地址;
typedef uint32_t in_addr_t;
struct in_addr {
in_addr_t s_addr;
};
返回值:
成功,返回非0;
失败,返回0;
只适用于IPV4;
#define IP "192.168.1.3" //0xC0A80103 --> 0x301A8C0
struct in_addr inp;
if(inet_aton(IP, &inp) == 0)
{
printf("失败\n");
return -1;
}
printf("%#X\n", inp.s_addr); //0x301A8C0
2)inet_pton
头文件:
#include <arpa/inet.h>
原型:
int inet_pton(int af, const char *src, void *dst);
参数:
int af:协议族
AF_INET IPV4
AF_INET6 IPV6
char *src:指定要转换成网络字节序的点分十进制字符串;
void* dst
typedef uint32_t in_addr_t;
struct in_addr {
in_addr_t s_addr;
};
af == AF_INETa;
struct in6_addr
{
}
返回值:
成功,返回1;
失败,返回0或者-1,更新errno;
#define IP "192.168.1.3" //0xC0A80103 --> 0x301A8C0
struct in_addr inp;
inet_pton(AF_INET, IP, &inp);
printf("%#X\n", inp.s_addr); //0x301A8C0
3)inet_addr
头文件:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
原型:
uint32_t inet_addr(const char *cp);
参数:
char *cp:指定要转换的点分十进制字符串。
返回值:
成功,返回转换成网络字节序的IP:in_addr_t
typedef uint32_t in_addr_t;
struct in_addr {
in_addr_t s_addr;
};
失败,返回-1;
只适用于IPV4;
2.网络字节序-->点分十进制
1)inet_ntoa
头文件:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
原型:
char *inet_ntoa(struct in_addr in);
参数:
struct in_addr in:存储IP网络字节序的结构体;
返回值:
成功,返回点分十进制字符串的首地址;
失败,返回NULL;
注意:
转换后的字符串是存储在静态存储区的,下一次再调用该函数,会把上一次的数据覆盖,所以最好保存函数返回的数据;
例子:
printf("%s\n", inet_ntoa(inp));
2)inet_ntop
头文件:
#include <arpa/inet.h>
原型:
const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
参数:
int af:
AF_INET IPV4
AF_INET6 IPV6
void *src:存储网络字节序的结构体指针; struct in_addr
char *dst:存储转换后的点分十进制的字符串;
socklen_t size:缓冲区的带下,其实就是指定多大的空间用于转换IP;
返回值:
成功,返回字符串的首地址,就是dst;
失败,返回NULL,更新errno;
char dst[20];
inet_ntop(AF_INET, &inp, dst, sizeof(dst));
printf("%s\n", dst);
819




被折叠的 条评论
为什么被折叠?



