然後htonl,htons,ntohs也介紹一下:
不同的cpu在內存中保存整數的方式不一樣
有的是低位在前 如intel cpu
有的是高位在前 如sun的 sparc
使用
char b[4];
*(int*)b = 0x10203040;
然後看b裏面的內容 就知道是什麼樣保存的了
這樣雙方定義標準 纔不會理解出錯
某些協議定義的數據存放格式不一致,
用這些函數(或宏)來轉換數據格式。
ntohl
network order to host byte order
ntohl function converts a u_long
Server
u_long htonl(u_long hostlong);
舉例:htonl(0)=0
htonl(80)= 1342177280
client
u_long ntohl(u_long netlong);
舉例:ntohl(0)=0
ntohl(1342177280)= 80
From: http://hi.baidu.com/cdmember_daihw/blog/item/ad83090f1fb4ffebab6457e6.html