我在win7 x64的visual studio 2015工作。
我尝试通过zmq_identity找到特定的客户端。但 不行。
void *context = zmq_ctx_new();
// Socket to talk to clients
zmq::socket_t* responder = (zmq::socket_t*)zmq_socket(context, ZMQ_REP);
zmq_setsockopt(responder, ZMQ_IDENTITY, "hell", 4);
zmq_connect(responder, "tcp://localhost:5560");
while (1) {
// Wait for next request from client
char buf[512];
zmq_recv(responder, buf, 512, ZMQ_RCVMORE);
zmq_recv(responder, buf, 512,0);
sleep(1);
zmq_send(responder, "B", 1, ZMQ_SNDMORE);
zmq_send(responder, "worldB",5, 0);
}
zmq_close(responder);
zmq_ctx_destroy(context);
return 0;
void *context = zmq_ctx_new();
// Socket to talk to server
void *requester = zmq_socket(context, ZMQ_REQ);
zmq_setsockopt(requester, ZMQ_IDENTITY, "A", 1);
zmq_connect(requester, "tcp://localhost:5506");
int request_nbr;
for (request_nbr = 0; request_nbr < 5; request_nbr++)
{
char buf[512];
zmq_send(requester, "Hello", 5, 0);
std::cout << "IN RECV" << std::endl;
int recvc = zmq_recv(requester, buf, 512, ZMQ_RCVMORE);
printf("Received reply %d [%s]\n", recvc, buf);
char buf2[512];
recvc = zmq_recv(requester, buf2, 512, 0);
printf("Received reply %d [%s]\n", recvc, buf2);
}
// Prepare our context and sockets
void *context = zmq_ctx_new();
void *frontend = zmq_socket(context, ZMQ_ROUTER);
void *backend = zmq_socket(context, ZMQ_DEALER);
zmq_bind(frontend, "tcp://*:5506");
zmq_bind(backend, "tcp://*:5560");
zmq_proxy(frontend, backend,NULL);
如果你运行它,你可以看到客户端获得标识符B的消息;
我想知道如何使用zmq身份设置客户端识别。
答案 0 :(得分:0)
如果您使用REP
套接字进行连接,则无法看到该身份。它被插座剥离。
您应该尝试使用DEALER
套接字进行连接,以查看身份框架。