title: How to create a IP Address in TCP/IP network byte order?
body: <p>I am using a raw boost socket to send TCP packets. (Compiling in VS2010)
  My problem is that the arguments of the IP header for socket.send_to() are not valid.</p>
  
  <p>I checked my own IP header and TCP header struct object and noticed the binary size is wrong. The problem relates to ip address fields for source and destination.</p>
  
  <p>I am entering the IP address: 192.168.1.1, 255.255.255.255 and it works fine.
  But if I offer the IP address 10.10.7.1 the binary result is wrong.</p>
  
  <p>So let's see the struct, which stores the IP as unsigned int value:</p>
  
  <pre><code>#pragma pack(push, 1)
  struct iphdrerer
    {
      uint32_t    saddr;
      uint32_t    daddr;
      uint32_t    baddr;
    };
  #pragma pack(pop) 
  </code></pre>
  
  <p>The IP needs to get convertet into binary format and big endian. I choose three different IP examples:</p>
  
  <pre><code>iphdrerer foo;
  
  foo.saddr = (uint32_t) htonl(  boost::asio::ip::address_v4::from_string("10.10.5.1").to_ulong() ) ;
  foo.daddr = (uint32_t) htonl(  boost::asio::ip::address_v4::from_string("192.168.1.5").to_ulong()) ;
  foo.baddr = (uint32_t) htonl(  boost::asio::ip::address_v4::from_string("255.255.255.255").to_ulong()) ;
  
  std::cout &lt;&lt; std::dec &lt;&lt; foo.saddr &lt;&lt; " HEX=&gt; " &lt;&lt; std::hex &lt;&lt; foo.saddr &lt;&lt; std::endl;
  std::cout &lt;&lt; std::dec &lt;&lt; foo.daddr &lt;&lt; " HEX=&gt; " &lt;&lt; std::hex &lt;&lt; foo.daddr &lt;&lt; std::endl;
  std::cout &lt;&lt; std::dec &lt;&lt; foo.baddr &lt;&lt; " HEX=&gt; " &lt;&lt; std::hex &lt;&lt; foo.baddr &lt;&lt; std::endl;
  
  FILE* fout = fopen("foo.txt", "w");
  fwrite(&amp;foo, sizeof(iphdrerer), 1, fout);
  fclose(fout);
  </code></pre>
  
  <p>The console output is this:</p>
  
  <pre><code>17107466 HEX=&gt; 1050a0a
  83994816 HEX=&gt; 501a8c0
  4294967295 HEX=&gt; ffffffff
  </code></pre>
  
  <p>And I exported the struct object as file and opened this in a HEX viewer.
  This is the result of foo.txt:</p>
  
  <blockquote>
    <p>0D 0A 0D 0A 05 01 C0 A8 01 05 FF FF FF FF</p>
  </blockquote>
  
  <p>These are 14 Bytes instead of just 12 Bytes. I removed padding by using  #pragma pack(push, 1). </p>
  
  <p>Also the hex values of the file are not matching with the console output. 
  The output should show me big endian address for TCP/IP.</p>