Portable way of sending 64-bit variable through POSIX socket
<p>I'm designing custom network protocol and I need to send <a href="http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/stdint.h.html#tag_13_47_03_01" rel="nofollow noreferrer"><code>uint64_t</code></a> variable (representing file's length in bytes) through <a href="http://man7.org/linux/man-pages/man3/socket.3p.html" rel="nofollow noreferrer"><code>socket</code></a> in <a href="https://en.wikipedia.org/wiki/Linux_kernel#Portability" rel="nofollow noreferrer">portable</a> and <a href="https://en.wikipedia.org/wiki/POSIX" rel="nofollow noreferrer">POSIX-compliant</a> manner.</p>

<p>Unfortunately <a href="http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/stdint.h.html#tag_13_47_03_01" rel="nofollow noreferrer">manual</a> says that integer types with width 64 are <strong>not guaranteed</strong> to exist:</p>

<blockquote>
  <p>If an implementation provides integer types with width 64 that meet these requirements, then the following types are required: <code>int64_t</code> <code>uint64_t</code></p>
</blockquote>

<p>What's more there is no POSIX-compliant equivalent of <a href="http://pubs.opengroup.org/onlinepubs/7908799/xns/htonl.html" rel="nofollow noreferrer"><code>htonl</code>, <code>htons</code>, <code>ntohl</code>, <code>ntohs</code></a> (note that <a href="http://man7.org/linux/man-pages/man3/bswap_64.3.html" rel="nofollow noreferrer"><code>bswap_64</code></a> <a href="https://stackoverflow.com/questions/3022552/is-there-any-standard-htonl-like-function-for-64-bits-integers-in-c">is <strong>not</strong></a> POSIX-compliant).</p>

<p>What is the best practice to send 64-bit variable through socket?</p>