htons

S

shalu

Hi,
Iam trying to understand Socket programming. I couldnt understand the
functions htons() and htonl(). What is the difference between host byte
order and network byte order ?

Thanks,
Shal
 
U

Ulrich Eckhardt

shalu said:
Iam trying to understand Socket programming. I couldnt understand the
functions htons() and htonl(). What is the difference between host byte
order and network byte order ?

Possibly, there is no difference. Else, they might differ in endianess,
look up 'big endian' or 'little endian' on google. Alternatively, look at
the implementations of those macros by a lib that's designed to be
portable to different platforms, like e.g. glibc.

Uli
 
S

Stephen Sprunk

shalu said:
Hi,
Iam trying to understand Socket programming. I couldnt understand the
functions htons() and htonl(). What is the difference between host byte
order and network byte order ?

"Network byte order" is defined to be big-endian; individual hosts might be
big- or little-endian. To deal with this, the macros/functions htons(),
htonl(), ntohs(), and ntohl() allow you to create code that automatically
changes endianness if necessary on a particular host platform.

Do note that in this context a "short" is defined to be exactly two octets
and a long to be exactly four octets. A C "short" or "long" might be larger
on a particular platform, but will never be smaller.

S
 
K

karl malbrain

shalu said:
Hi,
Iam trying to understand Socket programming. I couldnt understand the
functions htons() and htonl(). What is the difference between host byte
order and network byte order ?

Network byte order is always big-endian, where the most significant byte is
transmitted first in the network packet. Host byte order depends on your
CPU platform. On the x86 it is little-endian, where the least significant
byte is stored in memory first.

It's a little confusing, and the htons and htonl macros take care of sorting
this out for you on your platform automatically.

karl m
 
A

ankisharma

If the system's host byte order is bigendian then these macros are
defined to null otherwise they convert the input to big endian.
 
D

dandelion

If the system's host byte order is bigendian then these macros are
defined to null otherwise they convert the input to big endian.

^^^^^^^^

I think you intended "little endian"
 
A

ankisharma

Following is what i intended:
---------------------------------------------

Listing 1. Byte reordering macros #if defined(BIG_ENDIAN) &&
!defined(LITTLE_ENDIAN)

#define htons(A) (A)
#define htonl(A) (A)
#define ntohs(A) (A)
#define ntohl(A) (A)

#elif defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN)

#define htons(A) ((((uint16)(A) & 0xff00) >> 8) | \
(((uint16)(A) & 0x00ff) << 8))
#define htonl(A) ((((uint32)(A) & 0xff000000) >> 24) | \
(((uint32)(A) & 0x00ff0000) >> 8) | \
(((uint32)(A) & 0x0000ff00) << 8) | \
(((uint32)(A) & 0x000000ff) << 24))
#define ntohs htons
#define ntohl htohl

#else

#error "Either BIG_ENDIAN or LITTLE_ENDIAN must be #defined, but not
both."

#endif
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top