title: How to can I serialize data to Communication between C++ zmq client and Python zmq Server
body: <p><strong>UPDATING MY QUESTION</strong></p>
  
  <p>How to can I represent the arrived message in my python zmq server to show their content ?</p>
  
  <p>According to this behavior, can I assume that the btnState data is sent to python server in anyway?</p>
  
  <p>Context:</p>
  
  <p>I am sending some data members structures
   using a C++ zeromq client process:
  <code>ZMQComponent.h</code> file </p>
  
  <pre><code>#include &lt;zmq.hpp&gt;
  #include &lt;sofa/defaulttype/VecTypes.h&gt;
  
  // To Quat datatype
  #include &lt;sofa/defaulttype/Quat.h&gt;
  using sofa::defaulttype::Quat;
  
  using std::string;
  
  namespace sofa
  {
  
  namespace component
  {
  
  namespace controller
  {
  
  /* data structure which I want send data to python zmq server */
  struct instrumentData
  {
    typedef sofa::defaulttype::Vec3d Vec3d;
    Vec3d pos;
    Quat quat;
    int btnState;
    float openInst;
    bool blnDataReady;
  };
  
  class ZMQComponent : public sofa::core::behavior::BaseController
  {
    public:
      SOFA_CLASS(ZMQComponent, sofa::core::behavior::BaseController);
  
      ZMQComponent();
      virtual ~ZMQComponent();
  
      /* Conect to ZMQ external python Server  */
      void setupConnection();
  
      /* Send some data memeber instrumentData structure to ZMQ external Server  */
      void instrumentDataSend(instrumentData a);
  
      /* initialize function */
      void init();
  
  };
  
  } // namespace sofa
  
  } // namespace component
  
  } // namespace controller
  </code></pre>
  
  <p>The <code>ZMQComponent.cpp</code> is:</p>
  
  <pre><code>#include &lt;sofa/core/ObjectFactory.h&gt;
  #include &lt;zmq.hpp&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include "ZMQComponent.h"
  
  
  using namespace std;
  
  namespace sofa
  {
  
  namespace component
  {
  
  namespace controller
  {
  
  /*  ZMQ Internal Client context and socket */
  zmq::context_t context(1);
  zmq::socket_t socket(context, ZMQ_REQ);
  
  ZMQComponent::ZMQComponent(){}
  
  void ZMQComponent::setupConnection()
  {
      cout &lt;&lt; "Connecting to python zeroMQ server ..." &lt;&lt; endl;
      socket.connect("tcp://localhost:5555");
  }
  
  void ZMQComponent::instrumentDataSend(instrumentData a)
  {
      /*  Initialize the data members structure instrumentData */
      a.pos = sofa::defaulttype::Vec3d(1.0f, 1.0f, 1.0f);
      a.quat = defaulttype::Quat(1.0f, 1.0f, 4.0f, 1.0f);
      a.btnState = 5671;
      a.openInst = 1.0f;
      a.blnDataReady = false;
  
      string s, test, result, d;
      s = to_string(a.btnState);
      test = " is a number";
      result = s + test;
  
      /*  We send  the btnState data  */
      zmq::message_t request(30);
  
  
  
  /*  We ask for the memory address to ge the btnState content and send it. */
      memcpy(request.data(), &amp;result, 30);
      socket.send(request);
  }
  
  
  /*  In the init function we create the objects to setup connection and send data  */
  void ZMQComponent::init()
  {
      std::cout &lt;&lt; "ZeroMQCommunication::init()" &lt;&lt; std::endl;
      ZMQComponent z;
      z.setupConnection();
  
      instrumentData itemp;
      z.instrumentDataSend(itemp);
  
  }
  
  /*  Other code related ....  */
  ZMQComponent::~ZMQComponent(){}
  
  // int ZeroMqComponentClass = sofa::core::RegisterObject("This component does nothing.").add&lt;ZeroMqComponent&gt;();
  SOFA_DECL_CLASS(ZMQServerComponent)
  
  int ZMQServerComponentClass = sofa::core::RegisterObject("This component create a Socket.").add&lt; ZMQServerComponent &gt;();
  } // namespace controller
  
  } // namespace component
  
  } // namespace sofa
  </code></pre>
  
  <p>Then , my python zmq server  which receive the <code>btnState</code>  int variable is:</p>
  
  <pre><code>import time
  import zmq
  
  context = zmq.Context()
  socket = context.socket(zmq.REP)
  socket.bind("tcp://*:5555")
  print('ZMQ Server listening ... ')
  
  while True:
      #  Wait for next request from client
      message = socket.recv()
      print("Received message from Sofa: {}".format(message))
  
      #  Do some 'work'
      time.sleep(1)
  </code></pre>
  
  <p>The output or the message which arrive to python zmq server is the content of <code>result</code> variable (<code>btnState</code> turn to string in s content variable + string <code>test</code> concatenated) and some symbols characters of the :</p>
  
  <pre><code>(cnvss_test) ➜  Python git:(ZMQCommunication) ✗ python server.py
  ZMQ Server listening ...
  Received message from Sofa: b'\xb0\x1d\x19\xf4\xfd\x7f\x00\x00\x0e\x00\x00\x00\x00\x00\x00\x0045 is a number'
  </code></pre>
  
  <p>The previous output on my ZMQ python server script show that the string <code>result</code> from sofa is arrived to server, and their content is visualized, but too, this strings or characters symbols, which are product or consequence of the size of the <code>zmq::message_t request(30)</code> defined in my C++ client in the beginning.</p>
  
  <p>If I assign a value less than 30 in the request, by example <code>zmq::message_t request(10)</code> the output in my server is:</p>
  
  <pre><code>Received message from Sofa: b'\x90\x94\xa1\x00\xfc\x7f\x00\x00\x0e\x00'
  </code></pre>
  
  <p>If I assign a value greater than 10 in the request, by example <code>zmq::message_t request(20)</code> the output in my server is:</p>
  
  <pre><code>Received message from Sofa: b'\x80$(\xc7\xfc\x7f\x00\x00\x0e\x00\x00\x00\x00\x00\x00\x0045 i
  </code></pre>
  
  <p>Then, the string or object which I receive in the server side, it has as long as the length or size assigned to <code>zmq::message_t request</code> variable</p>
  
  <p>Based in the above mentioned, is ZMQ whom add this strings in my message received?</p>
  
  <p>According to the previous process, my message is arrived to my server, then is correct attempt what the serialization process with some entity like protocol buffer is necessary?
  I understand that use something like google protocol buffer allow have some correlation more controlled in relation to objects sent and the objects received in relation of their real content ...</p>
  
  <p>In any case, how to can I remove the strings or characters symbols that are added in the message arrived to the server?</p>
  
  <p>Any support or orientation will be highly appreciated </p>