title: ZeroMQ always sends and gets 1 byte (using the cppzmq bindings)
body: <p>The following server and client codes are slightly modified versions of  <a href="http://zguide.zeromq.org/page:all" rel="nofollow noreferrer">the official tutorial</a>. My interpretation of their outputs is that whatever they send or receive are always 1 byte. How can this happen?</p>
  
  <p>server.cpp:</p>
  
  <pre><code>#include &lt;zmq.hpp&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  #ifndef _WIN32
  #include &lt;unistd.h&gt;
  #else
  #include &lt;windows.h&gt;
  #define sleep(n) Sleep(n)
  #endif
  
  int main () {
      zmq::context_t context(1);
      zmq::socket_t socket(context, ZMQ_REP);
      socket.bind("tcp://*:5555");
      while(true){
          zmq::message_t request;
          int length=socket.recv(&amp;request);
          if(length &gt;= 0)
              std::cout &lt;&lt; length &lt;&lt; " bytes received:" &lt;&lt; std::string((char*)request.data(),length) &lt;&lt; ":" &lt;&lt; std::endl;
          sleep(1);
          zmq::message_t reply(5);
          memcpy(reply.data(), "World", 5);
          socket.send(reply);
      }
      return 0;
  }
  </code></pre>
  
  <p>server ouput:</p>
  
  <pre><code>1 bytes received:H:
  </code></pre>
  
  <p>client.cpp:</p>
  
  <pre><code>#include &lt;zmq.hpp&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  int main ()
  {
      zmq::context_t context(1);
      zmq::socket_t socket(context,ZMQ_REQ);
      socket.connect ("tcp://localhost:5555");
      zmq::message_t request(5);
      memcpy(request.data(),"Hello",5);
      std::cout &lt;&lt; socket.send(request) &lt;&lt; " bytes sent." &lt;&lt; std::endl;
      zmq::message_t reply;
      int length=socket.recv(&amp;reply);
      if(length &gt;= 0)
          std::cout &lt;&lt; length &lt;&lt; " bytes received:" &lt;&lt; std::string((char*)request.data(),length) &lt;&lt; ":" &lt;&lt; std::endl;
      return 0;
  }
  </code></pre>
  
  <p>client output:</p>
  
  <pre><code>1 bytes sent.
  1 bytes received:H:
  </code></pre>
  
  <p>Given such strange results, I start to suspect that I might misunderstand ZeroMQ's usage.  I have read this "<a href="http://zguide.zeromq.org/page:all" rel="nofollow noreferrer">
  Minor Note on Strings</a>" repeatedly:</p>
  
  <blockquote>
    <p>ZeroMQ doesn't know anything about the data you send except its size in bytes. That means you are responsible for formatting it safely so that applications can read it back.</p>
  </blockquote>
  
  <p>, which is followed by this sentence:</p>
  
  <blockquote>
    <p>So let's establish the rule that ZeroMQ strings are length-specified and are sent on the wire without a trailing null.</p>
  </blockquote>
  
  <p>These two paragraphs to me are contradictory. Exact what contents are sent by ZeroMQ?</p>