blob: d2075fd2c0de7670cedae994ec0877d0773ea148 [file] [log] [blame]
<html lang="en">
<head>
<title>Byte Order - The GNU C Library</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="The GNU C Library">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Internet-Namespace.html#Internet-Namespace" title="Internet Namespace">
<link rel="prev" href="Services-Database.html#Services-Database" title="Services Database">
<link rel="next" href="Inet-Example.html#Inet-Example" title="Inet Example">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<!--
This file documents the GNU C library.
This is Edition 0.12, last updated 2007-10-27,
of `The GNU C Library Reference Manual', for version
2.8 (Sourcery G++ Lite 2011.03-41).
Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002,
2003, 2007, 2008, 2010 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being ``Free Software Needs Free Documentation''
and ``GNU Lesser General Public License'', the Front-Cover texts being
``A GNU Manual'', and with the Back-Cover Texts as in (a) below. A
copy of the license is included in the section entitled "GNU Free
Documentation License".
(a) The FSF's Back-Cover Text is: ``You have the freedom to
copy and modify this GNU manual. Buying copies from the FSF
supports it in developing GNU and promoting software freedom.''-->
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
<link rel="stylesheet" type="text/css" href="../cs.css">
</head>
<body>
<div class="node">
<a name="Byte-Order"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Inet-Example.html#Inet-Example">Inet Example</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Services-Database.html#Services-Database">Services Database</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Internet-Namespace.html#Internet-Namespace">Internet Namespace</a>
<hr>
</div>
<h4 class="subsection">16.6.5 Byte Order Conversion</h4>
<p><a name="index-byte-order-conversion_002c-for-socket-1747"></a><a name="index-converting-byte-order-1748"></a>
<a name="index-big_002dendian-1749"></a><a name="index-little_002dendian-1750"></a>Different kinds of computers use different conventions for the
ordering of bytes within a word. Some computers put the most
significant byte within a word first (this is called &ldquo;big-endian&rdquo;
order), and others put it last (&ldquo;little-endian&rdquo; order).
<p><a name="index-network-byte-order-1751"></a>So that machines with different byte order conventions can
communicate, the Internet protocols specify a canonical byte order
convention for data transmitted over the network. This is known
as <dfn>network byte order</dfn>.
<p>When establishing an Internet socket connection, you must make sure that
the data in the <code>sin_port</code> and <code>sin_addr</code> members of the
<code>sockaddr_in</code> structure are represented in network byte order.
If you are encoding integer data in the messages sent through the
socket, you should convert this to network byte order too. If you don't
do this, your program may fail when running on or talking to other kinds
of machines.
<p>If you use <code>getservbyname</code> and <code>gethostbyname</code> or
<code>inet_addr</code> to get the port number and host address, the values are
already in network byte order, and you can copy them directly into
the <code>sockaddr_in</code> structure.
<p>Otherwise, you have to convert the values explicitly. Use <code>htons</code>
and <code>ntohs</code> to convert values for the <code>sin_port</code> member. Use
<code>htonl</code> and <code>ntohl</code> to convert IPv4 addresses for the
<code>sin_addr</code> member. (Remember, <code>struct in_addr</code> is equivalent
to <code>uint32_t</code>.) These functions are declared in
<samp><span class="file">netinet/in.h</span></samp>.
<a name="index-netinet_002fin_002eh-1752"></a>
<!-- netinet/in.h -->
<!-- BSD -->
<div class="defun">
&mdash; Function: uint16_t <b>htons</b> (<var>uint16_t hostshort</var>)<var><a name="index-htons-1753"></a></var><br>
<blockquote><p>This function converts the <code>uint16_t</code> integer <var>hostshort</var> from
host byte order to network byte order.
</p></blockquote></div>
<!-- netinet/in.h -->
<!-- BSD -->
<div class="defun">
&mdash; Function: uint16_t <b>ntohs</b> (<var>uint16_t netshort</var>)<var><a name="index-ntohs-1754"></a></var><br>
<blockquote><p>This function converts the <code>uint16_t</code> integer <var>netshort</var> from
network byte order to host byte order.
</p></blockquote></div>
<!-- netinet/in.h -->
<!-- BSD -->
<div class="defun">
&mdash; Function: uint32_t <b>htonl</b> (<var>uint32_t hostlong</var>)<var><a name="index-htonl-1755"></a></var><br>
<blockquote><p>This function converts the <code>uint32_t</code> integer <var>hostlong</var> from
host byte order to network byte order.
<p>This is used for IPv4 Internet addresses.
</p></blockquote></div>
<!-- netinet/in.h -->
<!-- BSD -->
<div class="defun">
&mdash; Function: uint32_t <b>ntohl</b> (<var>uint32_t netlong</var>)<var><a name="index-ntohl-1756"></a></var><br>
<blockquote><p>This function converts the <code>uint32_t</code> integer <var>netlong</var> from
network byte order to host byte order.
<p>This is used for IPv4 Internet addresses.
</p></blockquote></div>
</body></html>