message type is uint32. On my system size_t is 64 bits. 

Possible fix:
for( boost::uint32_t i =0; i < n; ++i )
...
if ( !out.write(reinterpret_cast<char *>(&i), sizeof(i)) )