473,385 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

htons


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

--
shalu
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

Nov 14 '05 #1
7 31062
shalu wrote:
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

Nov 14 '05 #2
"shalu" <sh**********@mail.codecomments.com> wrote in message
news:1102448048.8e5b922fd6d04db5ac9b28e8ac76f28d@t ng...

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

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking

Nov 14 '05 #3
"shalu" <sh**********@mail.codecomments.com> wrote in message
news:1102448048.8e5b922fd6d04db5ac9b28e8ac76f28d@t ng...

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
Nov 14 '05 #4
If the system's host byte order is bigendian then these macros are
defined to null otherwise they convert the input to big endian.

Nov 14 '05 #5

<an********@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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"

Nov 14 '05 #6
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

Nov 14 '05 #7

<an********@gmail.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
Following is what i intended:


I misread. Apologies.

<snip>
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Womble | last post by:
Hi, I need to work out the length of a message in network byte order. Does PHP have an equivalent function of htons and ntohs? I've searched all the PHP documentation but have so far drawn a...
8
by: Nico Grubert | last post by:
Hi there, I could not find any "ping" Class or Handler in python (2.3.5) to ping a machine. I just need to "ping" a machine to see if its answering. What's the best way to do it? Kind...
4
by: bobfilar | last post by:
I am working on a port enumerator and I am coming up with a problem. Here is the part of my code that is giving me fits right now: for (int i=0; i < (int) pTcpTable->dwNumEntries; i++) {...
2
by: ogedezvoltare | last post by:
ok a have the next code: /* we use a fork to create a child process that sniffs the answers */ switch(pid = fork()) { case -1: pe("Error fork"); exit(1); case 0: ...
7
by: apollo135 | last post by:
Dear all, Could someone please clarify and help with byte ordering macro (big/litte endian conversion)? I found thefollowinf macron on the internet but I have a doubt to use it ... ============...
3
by: apollo135 | last post by:
Dear all, Could someone please clarify and help with byte ordering macro (big/litte endian conversion)? I found thefollowinf macron on the internet but I have a doubt to use it ... ============...
4
by: iceman | last post by:
hi, which header files do i need to include for using htons in linux i had used #include <netinet/in.h> but i am still getting the error undefined reference to `htons(unsigned short)' ...
5
by: sam.barker0 | last post by:
Hi, How can I convert an unsigned char array containing IPV6 address into a string Eg if arrray contains 20 01 05 03 a8 3e 00 00 00 00 00 00 00 02 00 30 Then the address is Addr:...
2
by: sam.barker0 | last post by:
Hi guys, I am trying to form an IPV6 address string from the address bytes contained in a unsigned char buffer char tempstring; sprintf(tempstring, "%x:%x:%x:%x:%x:%x:%x:%x",htons(*((unsigned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.