您好我正在尝试理解并学习如何使用ZeroMQ,虽然这可能是基本的,但我没有找到解决方法。在这段代码中,我试图创建一个服务器来将消息从一个客户端转发到另一个客户端。服务器编译得很好,但有一些主要问题(几乎所有关于数据类型的问题)所以我甚至无法测试我的服务器。
server.cpp:
#include "zmq.hpp"
#include <string>
#include <iostream>
#include <unistd.h>
int main ()
{
zmq::message_t board (9);
zmq::context_t context (1);
zmq::socket_t socket1 (context, ZMQ_REP);
socket1.bind ("tcp://*:20000");
zmq::socket_t socket2 (context, ZMQ_REP);
socket1.bind ("tcp://*:20001");
int jogo=9;
while (jogo)
{
zmq::message_t request;
if (jogo%2!=0)
{
socket1.recv (&request);
memcpy ((void *) board.data (), request.data(), 9);
socket2.send (board);
}
else
{
socket2.recv (&request);
memcpy ((void *) board.data (), request.data(), 9);
socket1.send (board);
}
--jogo;
}
return 0;
}
jogo_client.cpp
#include "zmq.hpp"
#include <string>
#include <iostream>
int main ()
{
int player,port=20000,jogo=9,ganhou=0;
std::string game;
std::cin >> player;
port+=player;
zmq::context_t context (1);
zmq::socket_t socket (context, ZMQ_REQ);
std::cout << "Connecting to game server…" << std::endl;
//-std=gnu++11
socket.connect (std::string("tcp://localhost:")+std::to_string(port));
zmq::message_t board (9);
if(player)
{
while(jogo)
{
if (jogo%2!=0)
{
memcpy ((void *) board.data (), "Hi from 1", 9);
std::cout << "Sending Hi" << std::endl;
socket.send (board);
}
else
{
socket.recv (&board);
memcpy ((void *) ,game, request.data (), 9);
std::cout << "Got from 2: " << std::endl;
std::cout << game << std::endl;
}
--jogo;
}
}
else
{
while(jogo)
{
if (jogo%2!=0)
{
socket.recv (&board);
memcpy ((void *) ,game, request.data (), 9);
std::cout << "Getting from 2 " << std::endl;
std::cout << game << std::endl;
}
else
{
emcpy ((void *) board.data (), "Hi from 2", 9);
std::cout << "Sending Hi" << std::endl;
socket.send (board);
}
--jogo;
}
}
return 0;
}
编译错误:
jogo_client.cpp: In function ‘int main()’:
jogo_client.cpp:14:73: error: no matching function for call to ‘zmq::socket_t::connect(std::basic_string<char>)’
jogo_client.cpp:14:73: note: candidate is:
In file included from jogo_client.cpp:1:0:
zmq.hpp:384:21: note: void zmq::socket_t::connect(const char*)
zmq.hpp:384:21: note: no known conversion for argument 1 from ‘std::basic_string<char>’ to ‘const char*’
jogo_client.cpp:29:26: error: expected primary-expression before ‘void’
jogo_client.cpp:29:26: error: expected ‘)’ before ‘void’
jogo_client.cpp:43:26: error: expected primary-expression before ‘void’
jogo_client.cpp:43:26: error: expected ‘)’ before ‘void’