mercredi 19 août 2020

How to send/receive array of any datatype from client to server using zmqpp?

i currently play around with ZMQPP client server program. I attempt send an array of data

in client.cpp

int main(int argc, char *argv[])
{
  const std::string endpoint = "tcp://127.0.0.1:5555";
  const std::string  text = "helloWolrd!";
  int arr[5]= {5,4,3,2,1};
  // initialize the 0MQ context
  zmqpp::context context;

  // generate a push socket
  zmqpp::socket_type type = zmqpp::socket_type::push;
  zmqpp::socket socket (context, type);

  // open the connection
  std::cout << "Connecting to 0MQ server…" << std::endl;
  socket.connect(endpoint); 
  int action;
  

  zmqpp::message message;

  int  value=100;

  message <<text<<value<<arr;

  socket.send(message);

}

in server.cpp

#include <zmqpp/zmqpp.hpp>
#include <string>
#include <iostream>

using namespace std;

int main(int argc, char *argv[]) {
  const string endpoint = "tcp://127.0.0.1:5555";

  // initialize the 0MQ context
  zmqpp::context context;

  // generate a pull socket
  zmqpp::socket_type type = zmqpp::socket_type::pull;
  zmqpp::socket socket (context, type);

  // bind to the socket
  cout << "Binding to " << endpoint << "..." << endl;
  socket.bind(endpoint);

  // receive the message
  cout << "Receiving message..." << endl;
  zmqpp::message message;
  // decompose the message 
  socket.receive(message);
  cout<<"decomposed msg successfully"<<endl;
  string text;
  uint8_t number;
  int arr[5]= {0};
  message>>text>>number; //this will print hellowolrd and and 100 as expect
  // how to receive the array data?
  
  cout << "Received text:\"" << text<<endl;
  cout <<"number is"<<number<<endl;
  cout<< "arr[5]: is" << arr[5]<< endl;
  cout << "Finished." << endl;
}

At the moment i don't know how to accept the array data from client, also can my client program pack the array into message as shown below?

message <<text<<value<<arr; 

Please let me know if there is a better approach to send/receive array/vector of data from client to server using zmqpp.

Aucun commentaire:

Enregistrer un commentaire