htons函数具体解释
来源:互联网 发布:linux teamview 卸载 编辑:程序博客网 时间:2024/04/29 04:40
在Linux和Windows网络编程时需要用到htons和htonl函数,用来将主机字节顺序转换为网络字节顺序。
在Intel机器下,执行以下程序
int main()
{
printf("%d /n",htons(16));
{
printf("%d /n",htons(16));
return 0;
}
}
得到的结果是4096,初一看感觉很怪。
解释如下,数字16的16进制表示为0x0010,数字4096的16进制表示为0x1000。由于Intel机器是小尾端,存储数字16时实际顺序为1000,存储4096时实际顺序为0010。因此在发送网络包时为了报文中数据为0010,需要经过htons进行字节转换。如果用IBM等大尾端机器,则没有这种字节顺序转换,但为了程序的可移植性,也最好用这个函数。
另外用注意,数字所占位数小于或等于一个字节(8 bits)时,不要用htons转换。这是因为对于主机来说,大小尾端的最小单位为字节(byte)。
原文:http://blog.csdn.net/zouxinfox/archive/2007/10/07/1814088.aspx
- htons函数具体解释
- htons函数具体解释
- htons函数具体解释
- htons函数具体解释
- htons函数具体解释
- htons和htonl函数具体应用
- htonl(), ntohl(), htons(), ntohs() 函数具体应用
- htons函数
- htons()函数
- htons(), ntohl(), ntohs(),htons() 函数
- htons(), ntohl(), ntohs(),htons() 函数
- htons(), ntohl(), ntohs(),htons() 函数
- htons(), ntohl(), ntohs(),htons() 函数
- htons(), ntohl(), ntohs(),htons() 函数
- htons(), ntohl(), ntohs(),htons() 函数
- htons(), ntohl(), ntohs(),htons() 函数
- htons(), ntohl(), ntohs(),htons() 函数
- htons(), ntohl(), ntohs(),htons() 函数
- sizeof
- 最小生成树(minimum spanning tree)
- 新手上路
- JAVA线程池的简单实现及优先级设置
- INADDR_ANY
- htons函数具体解释
- 关于Tomcat虚拟主机域名的设置
- 从两道经典试题谈C/C++中联合体(union)的使用 10月 7th, 2006 by 宋宝华
- 大学毕业后的烦恼一
- Service起步
- AMBA、AHB、APB总线简介
- 迷茫
- Android/OPhone开发完全讲义---系统服务
- 模拟电子技术14 22------——放大电路 静态分析续