ZEROMQ客户端练习将接口与实现分开

zeroMQ client exercise separating interface from implementation

本文关键字:实现 接口 客户端 练习 ZEROMQ      更新时间:2023-10-16

我正在测试Zeromq Framework的示例异步库消息传递以在分布式应用程序中工作,从而使编程语言之间的互操作性。

我在此代码中编写以下C 客户端代码

#include <zmq.hpp>
#include <iostream>
#include <sys/time.h>
using std::cout;
using std::cin;
using std::endl;
using std::string;
int main ()
{
    //  Prepare our context and socket
    zmq::context_t context (1);
    zmq::socket_t socket (context, ZMQ_REQ);
    struct timeval t_ini, t_fin;
    gettimeofday(&t_ini, NULL);
    cout << "Connecting to zeroMQ python server" << endl;
    socket.connect ("tcp://localhost:5555");
    // Send data to server socket
    zmq::message_t request (20);
    memcpy (request.data (), "Hola Servidor Python", 20);
    socket.send (request);
    // Getting reply form zeroMQ python server
    zmq::message_t reply;
    socket.recv (&reply);
    string replyMessage = string(static_cast<char *>(reply.data()), reply.size());
    cout << "Getting reply form zeroMQ python server: " + replyMessage << " "  << endl;
    gettimeofday(&t_fin, NULL);
    printf("Tiempo en microsegundos: %ld microsegundosn",
           ((t_fin.tv_sec - t_ini.tv_sec)*1000000L
           +t_fin.tv_usec) -t_ini.tv_usec
          );
    return 0;
}

此C 客户端代码的作品

我的目标是重写以前的代码,该代码将接口与使用C 标头文件进行实现。

我有client.h文件,其中我定义了class Client

#ifndef ZEROMQ_CLASS_CLIENT_H
#define ZEROMQ_CLASS_CLIENT_H
#include <sys/time.h>
#include <iostream>
using std::string;
class Client{
public:
    Client();
    void setupConnection();
private:
    //zmq::context_t context (1);
    //zmq::socket_t socket (context, ZMQ_REQ);
    struct timeval t_before, t_after;
    string replyMessage;
};
#endif //ZEROMQ_CLASS_CLIENT_H

我有client.cpp文件,其中我开发了我的setupConnection()成员函数

#include "client.h"
#include <zmq.hpp>
using std::cout;
using std::endl;

Client::Client(){
};
void Client::setupConnection(){
    gettimeofday(&t_before, NULL);
    cout << "Connecting to python zeroMQ server ..."<<endl;
    zmq::context_t context(1);
    zmq::socket_t socket (context, ZMQ_REQ);
    socket.connect("tcp://localhost:5555");
    zmq::message_t request (20);
    memcpy(request.data(), "Hello Python server", 20);
    socket.send(request);
    zmq::message_t reply;
    socket.recv(&reply);
    replyMessage = string(static_cast<char *>(reply.data()), reply.size());
    cout << "Getting response from server: " + replyMessage << " " << endl;
    gettimeofday(&t_after, NULL);
    printf("Time in microseconds: %ld microsecondsn",
           ((t_after.tv_sec - t_before.tv_sec)*1000000L
            +t_before.tv_usec) -t_before.tv_usec
    );
}

main.cpp文件中,我创建对象并致电该函数:

#include "client.h"
int main() {
    Client c;
    c.setupConnection();
    return 0;
}

构建main.cpp文件时,我会收到此消息:

% g++ main.cpp -o client.out -lzmq
/tmp/cc8hDANf.o: In function `main':
main.cpp:(.text+0x20): undefined reference to `Client::Client()'
main.cpp:(.text+0x2c): undefined reference to `Client::setupConnection()'
collect2: error: ld returned 1 exit status

我无法弄清楚我的代码有什么问题,有什么想法吗?

当前,您不编译client.cpp。包括client.hpp还不够(显然这是一个单独的文件,您的编译器不假定有相应的.cpp文件。)尝试将client.cpp添加到您的GCC命令,即g++ main.cpp client.cpp -lzmq

您不编译客户端:

g main.cpp client.cpp -o client.out -lzmq