Some Noob Student
Some Noob Student

Reputation: 14574

Is it a must to reverse byte order when sending numbers across the network?

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?

Upvotes: 3

Views: 1210

Answers (7)

Alex Measday
Alex Measday

Reputation: 231

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.

Upvotes: 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

Upvotes: 1

gnometorule
gnometorule

Reputation: 2139

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.

Upvotes: 1

Jaseem
Jaseem

Reputation: 2285

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.

Upvotes: 1

Niklas Hansson
Niklas Hansson

Reputation: 473

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

Upvotes: 2

Brian Roach
Brian Roach

Reputation: 76898

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.

Upvotes: 3

ObscureRobot
ObscureRobot

Reputation: 7336

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.

Upvotes: 8

Related Questions