下载网址:https://zeromq.org/download/#windows
使用:
1、包含头文件
#include “zmq.h”
2、添加库
3、拷贝dll到工作目录下。
4、服务端程序
#include “stdafx.h”
#include “zmq.h”
int main()
{
int a, b, c;
zmq_version(&a, &b, &c);
unsigned char buf[128];
const char *content = "world";
void *ctx = zmq_ctx_new();
void *sc = zmq_socket(ctx, ZMQ_REP);
int rc = zmq_bind(sc, "tcp://127.0.0.1:5560");
if (rc != 0)
{
printf("error bind socket\n");
return 0;
}
printf("server start\n");
for (;;)
{
rc = zmq_recv(sc, buf, 32, 0);
if (rc > 0)
{
buf[rc] = 0;
printf("recv: %s\n", buf);
zmq_send(sc, content, strlen(content), 0);
}
}
rc = zmq_close(sc);
rc = zmq_ctx_term(ctx);
return 0;
}
5、客户端程序
#include “stdafx.h”
#include “zmq.h”
int main()
{
unsigned char buf[128];
const char *content = “hello”;
void *ctx = zmq_ctx_new();
void *sc = zmq_socket(ctx, ZMQ_REQ);
int rc = zmq_connect(sc, "tcp://127.0.0.1:5560");
for (int i = 0; i < 10; i++)
{
printf("send %d %s\n", i, content);
rc = zmq_send(sc, content, strlen(content), ZMQ_DONTWAIT);
rc = zmq_recv(sc, buf, 32, 0);
if (rc > 0)
{
buf[rc] = 0;
printf("recv: %s \n", buf);
}
Sleep(1000);
}
rc = zmq_close(sc);
rc = zmq_ctx_term(ctx);
return 0;
}