【发布时间】:2014-09-18 08:59:20
【问题描述】:
我需要在多个客户端之间进行通信。当我尝试运行文件(多个终端)时,我得到了相同的身份。所以我让路由器套接字自动设置UUID。但是我发现我不能使用该身份存储在服务器上以在多个客户端之间进行路由。
如何处理多个客户 ID?
我正在尝试构建一个异步聊天服务器。我正在遵循每个客户端的方法,经销商套接字连接到服务器( ROUTER-type sockets )。然后服务器提取客户端 ID(手动设置)并相应地读取消息和路由。
#include "zhelpers.hpp"
#include <iostream>
#include <string>
int main(void) {
zmq::context_t context(1);
zmq::socket_t backend (context, ZMQ_DEALER);
backend.setsockopt( ZMQ_IDENTITY, "mal2", 4);
backend.connect("tcp://localhost:5559");
std::string input;
std::cout <<"you are joinning" << std::endl;
while(1){
getline (std::cin, input);
s_send (backend, input);
zmq::pollitem_t items [] = {
{ backend, 0, ZMQ_POLLIN, 0 }
};
zmq::poll (items, 1, -1);
if (items [0].revents & ZMQ_POLLIN) {
std::string identity = s_recv (backend);
std::string request = s_recv (backend);//receive reply back from router which might be other client
std::cout<<"identity="<<identity<<"reques="<<request<<std::endl;
} //ending if
}//ending while
return 0;
}
【问题讨论】: