linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Use of htons function
@ 2004-09-20 16:08 Ronaldo Zacarias Afonso
  2004-09-20 16:41 ` Luciano Moreira - igLnx
  0 siblings, 1 reply; 3+ messages in thread
From: Ronaldo Zacarias Afonso @ 2004-09-20 16:08 UTC (permalink / raw)
  To: linux-c-programming

[-- Attachment #1: Mail message body --]
[-- Type: text/plain, Size: 452 bytes --]

  Hello all, 

  Some one could tell me what the function "htons" realy does? Where can I 
use it? 
  I tryed to find its purpose in the "man htons", but it seems a little bit 
confusing. 
  Thanks. 

_________________________________________________________________________________
Quer mais velocidade?
Só com o acesso Aditivado iG, a velocidade que você quer na hora que você precisa.
Clique aqui: http://www.acessoaditivado.ig.com.br


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Use of htons function
  2004-09-20 16:08 Use of htons function Ronaldo Zacarias Afonso
@ 2004-09-20 16:41 ` Luciano Moreira - igLnx
  0 siblings, 0 replies; 3+ messages in thread
From: Luciano Moreira - igLnx @ 2004-09-20 16:41 UTC (permalink / raw)
  To: Ronaldo Zacarias Afonso; +Cc: linux-c-programming

PORTUGUES Version: Se você fala português posso te explicar novamente.

ENGLISH Version:
When you re transmiting data over network, you sometimes don't know the 
application or hardware that are in the other side (peer).Thus, you need 
establish a standard for your data format.

Of course, when the data are characters, you could think that you dont 
need format definition, because characters whould be easy to undertand 
from other side. But it inst true, because the characters can be 
codified in ASCII, EBCDIC or another way. Then, you need to establish a 
standard for your data format.

Well, when the data are binary, like as a 16 bits integer instead of 5 
number characters.... samples representations for the number "42076":
a) Big-endian hardware this number will be: 0xA4 0x5C   (2 binary bytes)
b) Little-endian hardware this number will be: 0x5C 0xA4  (2 binary bytes)
c) Text ASCII this number will be: "42076" (5 ASCII text bytes)

As you can see, you have 3 or more ways to format your number, but the 2 
first are smaller and less expensive for networks when the amoung of 
fields data be large (example: a lot of fields of 16 bits or 32 bits).

Well, TCP/IP allow you a standard for binary traffic for 16 and 32 bits, 
and the funciton htons() is one of them, that convert a binary data of 
16 bits from host format to TCP/IP (network short) format. Thus, your 
peer needs to use nstoh() function to convert the 16 bits TCP/IP binary 
data to host format.

Some about big and little-endian:
- Intel is a Little-endian format
- PowerPC, mainframes, and others processor usually from Motorola or IBM 
are Big-endian.
- TCP/IP use Big-endian format, then, the use of the funtions like 
htons() on PowerPC or mainfraimes are optional, but isnt right to ommit 
them, because usually they arent functions, but MACROS defined by #define.

Regards,

Luciano



Ronaldo Zacarias Afonso wrote:

>  Hello all, 
>
>  Some one could tell me what the function "htons" realy does? Where can I 
>use it? 
>  I tryed to find its purpose in the "man htons", but it seems a little bit 
>confusing. 
>  Thanks. 
>
>_________________________________________________________________________________
>Quer mais velocidade?
>Só com o acesso Aditivado iG, a velocidade que você quer na hora que você precisa.
>Clique aqui: http://www.acessoaditivado.ig.com.br
>
>  
>

-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 3+ messages in thread

* RE: Use of htons function
@ 2004-09-20 18:14 Huber, George K RDECOM CERDEC STCD SRI
  0 siblings, 0 replies; 3+ messages in thread
From: Huber, George K RDECOM CERDEC STCD SRI @ 2004-09-20 18:14 UTC (permalink / raw)
  To: linux-c-programming

Ronaldo wrote:

>  Some one could tell me what the function "htons" realy does? Where can I 
>  use it? I tryed to find its purpose in the "man htons", but it seems a
little 
>  bit confusing. 

`htons' (host to network short) converts a short from the native byte-order
for 
platform to the "network byte-order".  The TCP/IP standard specifies 
that the network byte-order will be big-endian (i.e. the most significant
byte of
a mult-byte value will be stored in the lowest memory addrss).  

Based on this the overall effect of `htons' depends on the architecture of
the 
platform.  For example, if developing on an intel platform (which is
little-endian) 
then `htons' will convert to big-endian.  However, on a Sparc platform
(which is
big-endian) `htons' will do nothing.

You can use `htons' anywhere where you need a value in big-endian format.
You should 
use `htons' in any network related code when sending a short (say a port
number) across 
the network, regardless of the platform you are developing on - this aids
portability 
of your code.

Finally, you should be aware of the following functions:

htonl  -- host to network long
ntohs  -- network to host short
ntohl  -- network to host long

George


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2004-09-20 18:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-09-20 16:08 Use of htons function Ronaldo Zacarias Afonso
2004-09-20 16:41 ` Luciano Moreira - igLnx
  -- strict thread matches above, loose matches on Subject: below --
2004-09-20 18:14 Huber, George K RDECOM CERDEC STCD SRI

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).