linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Luciano Moreira - igLnx <lucianolnx@ig.com.br>
To: Ronaldo Zacarias Afonso <r_zaca@ig.com.br>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: Use of htons function
Date: Mon, 20 Sep 2004 13:41:22 -0300	[thread overview]
Message-ID: <414F0832.9000106@ig.com.br> (raw)
In-Reply-To: <20040920_160857_092073.r_zaca@ig.com.br>

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

  reply	other threads:[~2004-09-20 16:41 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-09-20 16:08 Use of htons function Ronaldo Zacarias Afonso
2004-09-20 16:41 ` Luciano Moreira - igLnx [this message]
  -- strict thread matches above, loose matches on Subject: below --
2004-09-20 18:14 Huber, George K RDECOM CERDEC STCD SRI

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=414F0832.9000106@ig.com.br \
    --to=lucianolnx@ig.com.br \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=r_zaca@ig.com.br \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).