3

In most of the examples on the web, authors usually change the byte order before sending a number from host byte order to network byte order. Then at the receiving end, authors usually restore the order back from network byte order to host byte order.

Q1:Considering that the architecture of two systems are unknown, wouldn't it be more efficient if the authors simply checked for the endianness of the machines before reversing the byte order?

Q2:Is it really necessary to reverse the byte order of numbers even if they are passed to & received by the same machine architecture?

Some Noob Student
  • 14,186
  • 13
  • 65
  • 103

7 Answers7

8

In general, you can't know the architecture of the remote system. If everyone uses a specific byte order - network byte order, then there is no confusion. There is some cost to all the reversing, but the cost of re-engineering ALL network devices would be far greater.

A1: Suppose that we were going to try to establish the byte order of the remote systems. We need to establish communication between systems and determine what byte order the remote system has. How do we communicate without knowing the byte order?

A2: If you know that both systems have the same architecture, then no you don't need to reverse bytes at each end. But you don't, in general, know. And if you do design a system like that, then you have made an network-architecture decision that excludes different CPU architectures in the future. Consider Apple switching from 86k to PPC to x86.

ObscureRobot
  • 7,306
  • 2
  • 27
  • 36
  • Regarding A1: You could always reverse the byte order during the initial communication before a common byte order is established, and then switch to the common byte order. The downside, of course, is that this introduces a fair bit of complexity for arguably very little gain. – Ethan Apr 13 '12 at 01:03
  • That works just fine until someone accidentally uses a endian-symmetric bit pattern to establish byte order. And then wonder why their communication is unreliable. – ObscureRobot Apr 13 '12 at 02:49
3

A1: No, that's the point. You don't want to have to care about the endienness of the machine on the other end. In most cases (outside of a strict, controlled environment ... which is most definitely not the internet) you're not going know. If everyone uses the same byte order, you don't have to worry about it.

A2: No. Except when that ends up not being the case down the road and everything breaks, someone is going to wonder why a well known best practice wasn't followed. Usually this will be the person signing off on your paycheck.

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
2

Little or big Endian is platform specific, but for network communication, it is common with big endianness, see wiki.

Niklas Hansson
  • 503
  • 2
  • 16
1

Q1: yes, it would be more efficient if sender & reciever tested their endianess (more precisely, communicate to each other their endianess, and tested if it is the same).

Q2: no, it is not always necessary to use a "standard" byte order. But it is simpler for code wanting to interoperate portably.

However, ease of coding might be more important than communication performance -and the network costs much more than swapping bytes, unless you've got a big lot of data.

Read about serialization and e.g. this question and my answer there

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • There is nothing like testing. It is a very know fact. You will have to know about your own computer i guess. – Jaseem Nov 17 '11 at 19:24
  • Endianess is known statically. On Linux, see include file `` – Basile Starynkevitch Nov 17 '11 at 19:32
  • Really? If I hand you an IP address, a protocol and a port, what is the native endianness? You might know your local endianness, but not remote -- unless you agree that all network communication should be in network byte order. – ObscureRobot Nov 17 '11 at 21:23
  • I mean that each machine knows its own endianess. So it could communicate it to the other end. (IIRC, the X11 protocol does that) – Basile Starynkevitch Nov 18 '11 at 06:38
1

Its not about blind reversing. All networks work on big endian. My computer [ linux + intel i386] work on little endian. so i always reverse the order when i code for my computer. i think mac work over big endian. Some mobile phone platforms also.

Jaseem
  • 2,236
  • 6
  • 28
  • 35
1

Network byte order is big-endian. If the sending or receiving architecture is big-endian too, you could skip the step on that end as the translation amounts to a nop. However, why bother? Translating everything is simpler, safer, and has close to no performance impact.

gnometorule
  • 2,151
  • 2
  • 20
  • 29
1

No need to test for endianness if you use the ntohl() and htonl(), etc. functions/macros. On big-endian machines, they'll already be no-ops.

Alex Measday
  • 231
  • 1
  • 2