- first install ZMQ , how to install see “python note “
注意在中文机器不能用这个命令 ; pip install pyzmq
因为有解码问题
- how to make a hello world in python :
https://learning-0mq-with-pyzmq.readthedocs.io/en/latest/pyzmq/basics.html#installation
- 先启动 c++ 的 ZMQ server , 然后运行这个脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import zmq import sys context = zmq.Context() print ("Connecting to server...") socket = context.socket(zmq.REQ) socket.connect ("tcp://localhost:5555") # Do 10 requests, waiting each time for a response #for request in range (10): socket.send_string("Hello") # Get the reply. message = socket.recv() print(message) #print ("Received reply ", request, "[", message, "]") |
- 以上只是传string, 怎么传 json
https://pyzmq.readthedocs.io/en/latest/serialization.html
-
- 没必要一定传json , 对于复杂的msg, 可以传递用空格分隔字符串, 比如
这样的字符串:aa bb cc dd ee ff gg hh …. 也足够用了
改造后的python client 如下 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import zmq import sys context = zmq.Context() print ("Connecting to server...") socket = context.socket(zmq.REQ) socket.connect ("tcp://localhost:5555") # Do 10 requests, waiting each time for a response #for request in range (10): socket.send_string("rb buy 3670") print ("done send msg") # Get the reply. message = socket.recv() print(message) #print ("Received reply ", request, "[", message, "]") |
c++ server code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
#include "C:\\Program Files (x86)\\ZeroMQ 2.2.0\\include\\zmq.hpp" #include <string> #include <iostream> #ifndef _WIN32 #include <unistd.h> #else #include <windows.h> #include <vector> #include <iostream> // std::cout #include <sstream> #define sleep(n) Sleep(n) #endif void dosth(const char* c1,const char* c2) { if(strcmp (c2,"buy")==0) return; if(strcmp (c2,"sell")==0) return; return; } int main () { // Prepare our context and socket zmq::context_t context (1); zmq::socket_t socket (context, ZMQ_REP); socket.bind ("tcp://*:5555"); while (true) { zmq::message_t request; // Wait for next request from client socket.recv (&request); //std::cout << "Received Hello" << std::endl; std::string rpl = std::string(static_cast<char*>(request.data()), request.size()); std::cout << rpl << std::endl; std::istringstream iss(rpl); std::vector<std::string> results((std::istream_iterator<std::string>(iss)), std::istream_iterator<std::string>()); int i = atoi(results[2].c_str()); dosth(results[0].c_str(),results[1].c_str()); // Do some 'work' sleep(1); // Send reply back to client zmq::message_t reply (5); memcpy (reply.data (), "World", 5); socket.send (reply); } return 0; } |
其中 split string 功能代码来自于:
https://www.fluentcpp.com/2017/04/21/how-to-split-a-string-in-c/
如何取出 message_t 里面的string 来自:
https://stackoverflow.com/questions/10901240/zeromq-how-to-access-tcp-message-in-c
PS : very important , after python client send , can not skip
1 2 |
message = socket.recv() print(message) |
because send and recv must be pair