Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I convert to network byte order (hton*) if all machines on the network use little-endian?

I am writing a client-server application that uses UDP to send datagrams between two Intel x64 computers. I control the hardware on both sides and have verified that they use identical little-endian architecture.

As I can confirm both machines are little-endian, are there any benefits given by using hton* functions to change the byte order before sending my data? Surely this creates a very slight performance decrease and gives no benefit?


1 Answers

Your protocol could always be used in ways you don't expect. The program may get picked up by someone running on an ARM or Sun system, for example.

Since values sent over a network are generally expected to be in network byte order, it's best to convert your values before sending. Any effect on performance would be negligible and would be considered premature optimization.

like image 146
dbush Avatar answered Feb 01 '26 05:02

dbush



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!