Host Byte Order & Network Byte Order

转载 2006年06月03日 16:08:00

Convert the Natives!

We've now been lead right into the next section. There's been too much talk about this Network to Host Byte Order conversion--now is the time for action!

All righty. There are two types that you can convert: short (two bytes) and long (four bytes). These functions work for the unsigned variations as well. Say you want to convert a short from Host Byte Order to Network Byte Order. Start with "h" for "host", follow it with "to", then "n" for "network", and "s" for "short": h-to-n-s, or htons() (read: "Host to Network Short").

It's almost too easy...

You can use every combination of "n", "h", "s", and "l" you want, not counting the really stupid ones. For example, there is NOT a stolh() ("Short to Long Host") function--not at this party, anyway. But there are:

htons() -- "Host to Network Short"

htonl() -- "Host to Network Long"

ntohs() -- "Network to Host Short"

ntohl() -- "Network to Host Long"
Now, you may think you're wising up to this. You might think, "What do I do if I have to change byte order on a char?" Then you might think, "Uh, never mind." You might also think that since your 68000 machine already uses network byte order, you don't have to call htonl() on your IP addresses. You would be right, BUT if you try to port to a machine that has reverse network byte order, your program will fail. Be portable! This is a Unix world! (As much as Bill Gates would like to think otherwise.) Remember: put your bytes in Network Byte Order before you put them on the network.

A final point: why do sin_addr and sin_port need to be in Network Byte Order in a struct sockaddr_in, but sin_family does not? The answer: sin_addr and sin_port get encapsulated in the packet at the IP and UDP layers, respectively. Thus, they must be in Network Byte Order. However, the sin_family field is only used by the kernel to determine what type of address the structure contains, so it must be in Host Byte Order. Also, since sin_family does not get sent out on the network, it can be in Host Byte Order.

 

字节顺序(byte order)

  • njnu_mjn
  • njnu_mjn
  • 2013年06月20日 23:37
  • 2736

网络带宽不足引起arc进程争用导致两个standby失败

一.背景概述 2014/08/17,按照客户的要求对异地dataguard进行实施工作。实施完成后拓扑结构及相关配置信息如下: 实施异地前同城standby可以正常应用日志且稳定运行时间超过...
  • Smasegain
  • Smasegain
  • 2015年07月21日 22:08
  • 2224

关于libtiff库函数的一些解释

原文出处:http://hi.baidu.com/necrohan/item/580ebe4a612b4408e9350464 关于Libtiff库 (1)下载tiff库文件 http://...
  • sd396791336
  • sd396791336
  • 2013年07月30日 16:25
  • 1960

字节顺序(byte order)

转自:http://blog.csdn.net/njnu_mjn/article/details/9141231 概念 字节顺序是指多字节的值在硬件中的存储顺序. 一般分为大端(big...
  • u011334621
  • u011334621
  • 2016年09月14日 17:58
  • 659

Byte and Bit Order Dissection

文章出处:http://www.linuxjournal.com/article/6788       Discussing the differences between big and lit...
  • ixidof
  • ixidof
  • 2012年08月02日 23:56
  • 1145

浅谈字节序(Byte Order)及其相关操作

 最近在为Tokyo Tyrant写一个.NET客户端类库。Tokyo Tyrant公开了一个基于 TCP协议的二进制协议,于是我们的工作其实也只是按照协议发送和读取一些二进 制数据流而已,并不麻烦。...
  • learningaginlearnin
  • learningaginlearnin
  • 2011年05月20日 09:14
  • 390

UTF-8编码格式的Byte Order Mark问题

前两天同事编写的SQL Server数据库脚本文件交给我运行时,出现了syntax error的错误,但将文件内容拷贝到SQL Server Management Studio里面运行时却一切正常。。...
  • zhuihunke_2008
  • zhuihunke_2008
  • 2016年12月01日 15:47
  • 195

字节顺序(byte order)

  • njnu_mjn
  • njnu_mjn
  • 2013年06月20日 23:37
  • 2736

字符集编码 Unicode UTF8 UTF16 UTF32 和BOM(Byte Order Mark)

BOM(Byte Order Mark)在分析unicode之前,先把bom(byte order mark)说一下。 bom是unicode字符顺序的标识符号,一般以魔数(magic code)的...
  • rodbate
  • rodbate
  • 2017年07月29日 12:58
  • 221

UTF-8文件的Unicode签名BOM(Byte Order Mark)

近日在调测一个UTF8编码的中文Zen Cart网站时遇到一件怪事,网页显示文字正常,用ie的察看源文件(记事本打开)却发现乱码,firefox没有这个问题。经在网上多方查证和多次测 试,解决了这个问...
  • davidsu33
  • davidsu33
  • 2014年05月31日 22:27
  • 1043
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Host Byte Order & Network Byte Order
举报原因:
原因补充:

(最多只允许输入30个字)