Search code examples
c#javac++network-programmingprogramming-languages

endians, languages, hardwares and network


We know the endian is related to the way how computers store data. Big endian computer architectures include the IBM 370, the Motorola 68000 and Sun Sparc. Little endian computers include the intel series (80486, pentium etc) and VAX.

Java is always Big-Endian because of the JVM. Network should always be Big-Endian because of the protocol.

  1. C, C++ and C# depand on the computer they are running?
  2. Network should always be Big-Endian because of the protocol. how about if we don't call htons and htonl before we send? The data sent across will be Little-endian if the sender is C++ on an intel machine. Is it right?
  3. So we don't need to care about the endian (call ntohl and htonl), if we know all the clients and server will use computers with the same architectures and will use the same program language. is it right?

Solution

    1. For C and C++, at least, yes; the endianness typically depends on the machine (but may also depend on the compiler). For C#, I don't know.
    2. Many network protocols are big-endian, yes. If you don't call htonl, then you will not be creating a valid packet on a little-endian machine.
    3. So you should always call htonl, etc. (or the equivalent in whichever language you're using). Because if even if you have a homogeneous environment today, it's almost certain that in the future, this will change.

    More specifically, you should always do the conversion as close to the interface as you can, and in one place. If you have endianness conversion calls strewn across your codebase, it becomes difficult to reason about whether your code is sane or not.