文章内容:
应答模式:REQ(客户端)和REP(服务端)
在使用网上的例子的时候出现了一种比较神奇的现象:
客户端和服务器的代码运行起来后没有按照一问一答的形式运行。后来发现是没有理解“一问一答”的含义。客户端和服务器必须一来一回的进行。如果客户端或者服务器没有发送或者没有接收消息则会卡死。
代码示例:
server:
h文件
#pragma once
#include <iostream>
#include "zmq.h"
class Server
{
public:
Server();
~Server();
void SendMsg();
private:
void* m_context = nullptr;
void* m_subscriber = nullptr;
};
cpp文件
#include "server.h"
#include <string>
Server::Server()
{
m_context = zmq_ctx_new();
m_subscriber = zmq_socket(m_context, ZMQ_REP);
int rc = zmq_bind(m_subscriber, "tcp://127.0.0.1:8080");
if (rc != 0)
{
printf("Socket binding failed\n");
// return -1;
}
}
Server::~Server()
{
}
void Server::SendMsg()
{
char buffer[256];
memset(buffer, 0, sizeof(buffer) - 1);
zmq_recv(m_subscriber, buffer, sizeof(buffer) - 1, 0); // 接收消息
printf("Received message: %s\n", buffer);
zmq_sleep(1);
strcpy_s(buffer, "World"); // 准备回复消息
zmq_send(m_subscriber, buffer, strlen(buffer), 0); // 发送回复消息
}
调用:
#include "server.h"
int main()
{
std::shared_ptr<Server> p_server = std::make_shared<Server>();
if (p_server)
{
while (1)
{
p_server->SendMsg();
}
}
return 0;
}
client:
调用:
#include "Clinet.h"
#include <iostream>
int main()
{
std::shared_ptr<Client> p_server = std::make_shared<Client>();
if (p_server)
{
while (1)
{
p_server->SendMsg();
}
}
return 0;
}
cpp文件:
#include "Clinet.h"
#include <string>
Client::Client()
{
m_context = zmq_ctx_new();
// 客户端
m_subscriber = zmq_socket(m_context, ZMQ_REQ);
// zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE, "", 0);
int rc = zmq_connect(m_subscriber, "tcp://127.0.0.1:8080"); // 连接到服务器端的地址和端口
if (rc != 0)
{
printf("connect error %s\n", zmq_strerror(errno));
zmq_close(m_subscriber);
zmq_ctx_destroy(m_context);
}
}
Client::~Client()
{
}
void Client::SendMsg()
{
// std::cout << "sendMsg" << std::endl;
int rc;
// 3.循环发送数据、接收数据
char str_tmp[] = "Hello, ZeroMQ!";
char* msg = str_tmp;
rc = zmq_send(m_subscriber, msg, strlen(msg), 0); // 发送消息
if (rc == -1)
{
printf("send error: %s\n", zmq_strerror(errno));
}
char buffer[256];
memset(buffer, 0, sizeof(buffer) - 1);
zmq_recv(m_subscriber, buffer, sizeof(buffer) - 1, 0); // 接收消息
printf("Received message: %s\n", buffer);
}
H文件:
#pragma once
#include "zmq.h"
class Client
{
public:
Client();
~Client();
void SendMsg();
private:
void* m_context = nullptr;
void* m_subscriber = nullptr;
};