Big endian or Little endian on net?

有些话、适合烂在心里 提交于 2019-12-03 11:15:25

问题


In what byte order does data transfer occur on net? Is it Little Endian or big endian? How is it converted to the respective byte order once the data reaches the host ?


回答1:


"Network byte order" is Big Endian, and protocols such as TCP use this for integer fields (e.g. port numbers). Functions such as htons and ntohs can be used to do conversion.

The data itself doesn't have any endianness it's entirely application defined, unless you're using a Presentation Layer such as XDR.




回答2:


Its transferred in whatever order you send it.

Traditionally, internet protocols use big endian, because the machines doing most of the communication were big endian.

However, if you define your own structures to send across the net, there is no need to follow that convention.

With C programming, typically, one often uses the htons or ntohs macros to do the conversion.




回答3:


if you are using tcp using htons((short)port) enables you to use network safe protocol(basically big endian) and dont care about little vs. big endian.

if you do need to convert use http://www.codeguru.com/forum/showthread.php?t=292902

Its explained well



来源:https://stackoverflow.com/questions/997571/big-endian-or-little-endian-on-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!