以下是一个使用ZeroMQ库的C语言代码示例,该代码创建一个ZMQ的REP套接字,连接到5555端口,并接收消息:
#include <zmq.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>int main (void)
{void *context = zmq_ctx_new();void *responder = zmq_socket(context, ZMQ_REP);int rc = zmq_bind(responder, "tcp://*:5555");if (rc != 0) {printf("Error occurred during zmq_bind(): %s\n", zmq_strerror(errno));return -1;}while (1) {char buffer[10];zmq_recv(responder, buffer, 10, 0);printf("Received: %s\n", buffer);sleep(1); // Do some workzmq_send(responder, "World", 5, 0);}return 0;
}
这个程序会一直运行,接收来自5555端口的消息,并打印出来。每接收到一个消息,它会等待1秒,然后发送一个"World"的回复。
分享一个有趣的 学习链接:https://xxetb.xet.tech/s/HY8za