python 发布
import zmq
from random import randrange
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5556")#这里一般不改
while True:
zipcode =1
temperature = 2
relhumidity =2
socket.send_string(f"{zipcode} {temperature} {relhumidity}")#传输三个信息
c++接收
#include <zmq.hpp>
#include <iostream>
#include <sstream>
int main (int argc, char *argv[])
{
zmq::context_t context ;
// Socket to talk to server
std::cout << "Collecting updates from weather server...\n" << std::endl;
zmq::socket_t subscriber (context, zmq::socket_type::sub);
subscriber.connect("tcp://localhost:5556");//改成你要订阅的ip地址,将localhost换成ip地址,后面的:5556是端口号,得和前面的程序对接上.即不用改.
// Subscribe to zipcode, default is NYC, 10001
const char *filter = (argc > 1)? argv [