Back to TILs

C++ zeromq_req_rep_server

Date: 2020-12-23Last modified: 2023-12-22

Table of contents

#include <spdlog/spdlog.h>

// #include <future>
// #include <iostream>
#include <string>

using namespace std;

#include <cppzmq/zmq.hpp>
#include <cppzmq/zmq_addon.hpp>

int main()
{
  zmq::context_t ctx;
  zmq::socket_t  repSocket( ctx, zmq::socket_type::rep );
  repSocket.bind( "tcp://*:15555" );

  int counter = 0;
  while( true ) {
    zmq::message_t requestMessage;

    repSocket.recv( requestMessage );
    spdlog::info( "Mensagem recebida" );

    // Realiza algum trabalho
    sleep( 1 );

    auto response = repSocket.send( zmq::str_buffer( "RESPONSE " ), zmq::send_flags::dontwait );
  }
  return 0;
}

Possible output

xxx

References