Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Simple but confusing question for a beginner.

Suppose A is a BE machine and is sending 0x44332211 sending data to B which is LE. By default A will be using htonl of 0x44332211 which is nothing but 0x44332211.

Now when 0x44332211 reaches B,B knows that its a BE format, so B will flip by using htonl function and change it to 0x11223344.

What does this flipping meeans? I read this concept from internet. Why flipping is required before storing?

Because in that case flipped value 0x11223344 will be stored in LE like 0x44332211,which is different from what A send because B interprets it differently though they look alike?

/* Conversion from BE to LE */

share|improve this question

2 Answers 2

The htonl() and ntohl() functions in a big endian C library actually don't do anything (they are "no-op"s). This way the same code can be used on both BE and LE machines; compiled with the C lib on the LE machine, those functions will flip the bytes, but on the BE machine, they will leave them the same, since network byte order is big endian, and the host is big endian already.

In other words, in your example, BE machine A never changes anything. Only the LE machine B does.

share|improve this answer
    
Ya goldilocks.Why LE machine ie B flips the data received?Why cant it store as such. –  Subi Suresh Apr 22 '13 at 17:51
    
Because it is little endian, so that is the way it stores data. If it doesn't do that, when that value is used subsequently, it will be wrong because the bytes are backward. Why there are the two styles is I guess a historical question...presumably big endian existed first en.wikipedia.org/wiki/Little_endian#History but little endian is a hardware level optimization. –  goldilocks Apr 22 '13 at 17:56
    
> i am summarising from the inputs you have given.What ever the machine is always the data is stored as byte 0 1st,byte 2 second and so on.Since B is a LE,it needs to store in the reverse order of A.so we are using htons(), so that byte 0 will be 11 byte1 will be 22 ,byte2 will be 33 and byte 4 will 44.Am i right?no need to worry about the internal representations in LE and BE. –  Subi Suresh Apr 22 '13 at 18:01
    
Yes, as long as you use htonl/ntohl before transmitting/after receiving, you don't have to worry about the internal representation. Even though most machines on the internet are probably LE, BE is network order to ensure consistent methodology. Remember, all this only applies to addresses used in IP packet headers -- the actual data in an IP transmission is structured according to whatever higher level protocols are used. So it's not everything, it's just specific bits (the address, the port number). –  goldilocks Apr 22 '13 at 18:09

Actually, flipping is not required before storage in a specific manner. It is required because the host uses the little endian order so when it receives a number longer than one byte from the network, it flips the bytes to interpret that number correctly. It is necessary doing this flipping before the data is used for whichever purpose it can be.

As an analogy, imagine yourself reading a manga printed in the original japanese way (i.e. the page usually being the last being actually the first one). If you're not aware of this fact, and don't "flip" the book, you're just unable to understand the story.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.