我是靠谱客的博主 拼搏大炮,最近开发中收集的这篇文章主要介绍zmq: request-reply multiple clients to multiple server, brute force way,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
/**
g++ req-res-N2M-brute-force-server.cpp -lzmq -g -O0 -o objs/req-res-N2M-brute-force-server
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zmq.h>
#include <assert.h>
int main(int argc, char** argv){
void* context = zmq_ctx_new();
void* responder = zmq_socket(context, ZMQ_REP);
char id[256];
memset(id, 0, sizeof(id));
assert(argc > 1);
for(int i = 1; i < argc; i++){
const char* port = argv[i];
char bind[64];
snprintf(bind, sizeof(bind), "tcp://*:%s", port);
printf("responder bind at: %sn", bind);
assert(zmq_bind(responder, bind) == 0);
strcat(id, port);
strcat(id, ",");
}
while(true){
zmq_msg_t msg;
zmq_msg_init(&msg);
zmq_msg_recv(&msg, responder, 0);
printf("get message: %sn", zmq_msg_data(&msg));
zmq_msg_t reply;
zmq_msg_init_size(&reply, strlen((char*)zmq_msg_data(&msg))+1+strlen(id));
memcpy(zmq_msg_data(&reply), id, strlen(id));
memcpy((char*)zmq_msg_data(&reply) + strlen(id), zmq_msg_data(&msg), strlen((char*)zmq_msg_data(&msg))+1);
zmq_msg_send(&reply, responder, 0);
zmq_msg_close(&reply);
zmq_msg_close(&msg);
}
zmq_close(responder);
zmq_ctx_destroy(context);
return 0;
}
/**
g++ req-res-N2M-brute-force-client.cpp -lzmq -g -O0 -o objs/req-res-N2M-brute-force-client
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zmq.h>
#include <assert.h>
int main(int argc, char** argv){
void* context = zmq_ctx_new();
void* requester = zmq_socket(context, ZMQ_REQ);
assert(argc > 1);
for(int i = 1; i < argc; i++){
char endpoint[64];
snprintf(endpoint, sizeof(endpoint), "tcp://localhost:%s", argv[i]);
assert(zmq_connect(requester, endpoint) == 0);
}
srand(0);
while(true){
char buf[64];
snprintf(buf, sizeof(buf), "msg, rand=%d", rand());
zmq_msg_t msg;
zmq_msg_init_size(&msg, strlen(buf)+1);
memcpy(zmq_msg_data(&msg), buf, strlen(buf)+1);
zmq_msg_send(&msg, requester, 0);
zmq_msg_close(&msg);
zmq_msg_t reply;
zmq_msg_init(&reply);
zmq_msg_recv(&reply, requester, 0);
printf("get a message:%sn", zmq_msg_data(&reply));
zmq_msg_close(&reply);
usleep(rand()%100 * 1000);
}
zmq_close(requester);
zmq_ctx_destroy(context);
return 0;
}
运行:
./objs/req-res-N2M-brute-force-server 1990 1991 1992 1993
./objs/req-res-N2M-brute-force-client 1990 1992
最后
以上就是拼搏大炮为你收集整理的zmq: request-reply multiple clients to multiple server, brute force way的全部内容,希望文章能够帮你解决zmq: request-reply multiple clients to multiple server, brute force way所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复