how python work as ZMP client

  • 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 , 然后运行这个脚本

  • 以上只是传string, 怎么传 json

https://stackoverflow.com/questions/37765847/send-json-object-through-zmq-with-c-client-python-server

https://pyzmq.readthedocs.io/en/latest/serialization.html

 

    • 没必要一定传json , 对于复杂的msg, 可以传递用空格分隔字符串, 比如

这样的字符串:aa bb cc dd ee ff gg hh …. 也足够用了

改造后的python client 如下 :

 

 

c++ server code :

 

其中 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 

because send and recv must be pair 

 

c++ create ZMQ client and server

download  Stable Release 2.2.0 win32 version 

  • install on windows PC
  • in visual studio , create win32 console app , for server side ,use code


and for client side , use code :

 

  • copy libzmq-v100-mt.lib and libzmq-v100-mt.dll to source.cpp folder

in linker—–>input , add libzmq-v100-mt.lib 

  • compile and run .

 

 

ZMQ 各种 socket 介绍

    • router – dealer 模式

为什么有  router – dealer 模式 ?      处理多client , 多service 的情况

例如下图:

1个client,多个service , 最简单的设计方法是: connect each client socket to multiple service endpoints

在这种情况下:

One client socket can connect to multiple service sockets, and the REQ socket will then distribute requests among these services. Let’s say you connect a client socket to three service endpoints; A, B, and C. The client makes requests R1, R2, R3, R4. R1 and R4 go to service A, R2 goes to B, and R3 goes to service C.

This design lets you add more clients cheaply. You can also add more services. Each client will distribute its requests to the services. But each client has to know the service topology. If you have 100 clients and then you decide to add three more services, you need to reconfigure and restart 100 clients in order for the clients to know about the three new services.

不好的地方是: client必须知道所有的service , 当新增若干个service,每一个client 都要修改并重启

router 或者 dealer 就是在client 和 service 之间的中间层  , 用 zmq_poll() 实现: What we want is something sitting in between clients and services that centralizes all knowledge of the topology. Ideally, we should be able to add and remove services or clients at any time without touching any other part of the topology.

实现示例图是这样的:

 

In this simple extended request-reply pattern, REQ talks to ROUTER and DEALER talks to REP.

 

  • 来源文档:

http://zguide.zeromq.org/page:all