socker地址API

大端字节序是指一个整数的高位字节存储在内存的低地址处,低位字节存储在内存的高地址处。小端字节序是指整数的高位字节存储在内存的高地址处,低位字节则存储在内存的低地址处。

现代pc大多采用小端字节序,故小端字节序又被称为主机字节序

发送端总是把要发送的数据转化为大端字节序数据后再发送,而接收端知道对方传送过来的数据总是采用大端字节序,所以接收端可以根据自身采用的字节序决定是否对接收到的数据进行转换,故大端字节序被称为网络字节序。

linux提供了如下4个函数来完成主机字节序和网络字节序之间的转换:

#include <netinet/in.h>
unsigned long int htonl(unsigned long int hostlong);//ip地址
unsigned short int htons(unsigned short int hostshort);//端口号
unsigned long int ntohl(unsigned lont int netlong);
unsigned short int ntohs(unsigned short int netshort);
原文地址:https://www.cnblogs.com/ljygoodgoodstudydaydayup/p/5245011.html