如何在 ZeroMQ 套接字中检索和存储随机 UUID? [英] How could I retrieve and store random UUID-s in ZeroMQ sockets?
问题描述
我需要在多个客户端之间进行通信.当我尝试运行文件(多个终端)时,我得到了相同的身份.所以我让路由器socket自动设置UUID.但是我发现我无法使用该身份在服务器上存储以在多个客户端之间进行路由.
I need to communicate between multiple clients. When I try to run file (multiple terminals) I get same identity. So I let router socket to automatically set the UUID. But what I found I cannot use that identity to store at server for routing between multiple clients.
我将如何处理多个客户 ID?
How would I handle multiple clients IDs?
我正在尝试构建一个异步聊天服务器.我正在遵循每个客户端的方法,经销商套接字连接到服务器( ROUTER
-type sockets ).然后服务器提取客户端 ID(手动设置)并相应地读取消息和路由.
I am trying to build an asynchronous Chat server. I am following an approach of each client with dealer socket connects to server ( ROUTER
-type sockets ). Server then extract the clients IDs ( set manually ) and reads the message and route accordingly.
#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;
}
推荐答案
Memento
几十年来已经证明,宁可根据所有收集到的需求来设计系统,即在编码之前,而不是相反.
Memento
It has been proved by decades to rather design a system based on all collected requirements, i.e. before coding, not vice versa.
通过这种方式,您的分析将在决定消息传递和信令层之前列出所有要求,避免出现我还想添加这个和那个......"
This way your analysis will list all the requirements before deciding on messaging and signalling layer, avoiding situations "And also I want to add this and that ..."
如果您决定在每个客户端创建一次性或持久的 UUID
-s,您将面临确保时间唯一性和安全性的挑战.随机化.
If you decide to create a disposable or persistent UUID
-s on each client side, you face a challenge to ensure both temporal uniqueness & randomisation.
如果您决定从 ChatSERVER 端分配 UUID
-s,除了聊天传输之外,您还需要一个额外的信令层.
If you decide to assign UUID
-s from a ChatSERVER side, you need an additional signalling layer, besides the chat-transport.
如果没有您肯定有的其他要求(或者迟早会意识到要面对他们,即时),我将如何处理多个客户 ID"的答案是无法回答的
Answer to "How would I handle multiple clients ID" is not answer-able without the other requirements you are sure to have ( or will sooner or later realise to face 'em, on-the-fly )
如上所述,好的项目始于适当而彻底的需求工程 &验证.与基于 Aha 和故障排除的响应式工程"相比,产品编码所需的时间最少.
As said above, good projects start with proper and thorough requirements engineering & validation. The product coding than spans a minimum amount of time, compared to a "Aha-based & trouble-shooting responsive engineering".
这篇关于如何在 ZeroMQ 套接字中检索和存储随机 UUID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!