有什么方法可以判断 ZeroMQ 消息来自哪里? [英] Is there any way to tell where a ZeroMQ message came from?

查看:33
本文介绍了有什么方法可以判断 ZeroMQ 消息来自哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ZeroMQ 套接字,它从不同机器上的多个进程接收数据.在不改变数据内容的情况下,有没有办法识别数据的来源?具体来说,如果来自 TCP 连接,我想要发件人的 IP 地址.

I have a ZeroMQ socket that is receiving data from multiple processes on different machines. Without changing the contents of the data, is there any way to identify the source of the data? Specifically, I'd like the IP address of the sender if it came from a TCP connection.

推荐答案

不,没有办法从 ZeroMq 套接字获取发件人 IP.该信息隐藏在 ZeroMq 的实现层中.您有多种选择来解决这个问题,一种是更改正在传递的消息并将发件人 IP 添加到消息本身,另一种是使用多部分消息.

No, there is no way to get the senders IP from the ZeroMq socket. That information is hidden within the implementation layers of ZeroMq. You have a couple of choices to handle solve this, one is to change the message being passed and simply add the senders IP to the message itself, another is to use Multi-Part messages.

来自 ZeroMq zmq_send() Api 文档 (3.2.2):

From the ZeroMq zmq_send() Api docs (3.2.2):

一个ØMQ消息由1个或多个消息部分组成.每个消息部分本身就是一个独立的 zmq_msg_t.ØMQ 确保消息的原子传递:对等方要么接收消息的所有消息部分,要么根本不接收.除可用内存外,消息部分的总数没有限制.

A ØMQ message is composed of 1 or more message parts. Each message part is an independent zmq_msg_t in its own right. ØMQ ensures atomic delivery of messages: peers shall receive either all message parts of a message or none at all. The total number of message parts is unlimited except by available memory.

多部分消息实际上是原子消息,但分成几个逻辑消息.IE.您收到所有零件或不收到零件.如果您无法修改原始消息,则可以在消息(在发送方端)前面加上发送方的 IP.然后接收方可以将第一部分提取为发送方 IP,将第二部分提取为原始的、未修改的消息.它将作为单个消息传递,但在逻辑上分为两个谨慎的部分.

Multi-Part messages are actually atomic messages but separated into several logical messages. I.e. you receive all parts or no parts. If you cannot modify the original message, you can prepend the message (on the sender side) with the IP of the sender. The receiver can then extract the first part as the senders IP and the second part as the original, unmodified, message. It will be delivered as a single message but is logically separated into two discreet parts.

在你的情况下,你可以这样做:

In your case, you could do something like this:

// Send a multi-part message consisting of sender IP plus another message
zmq_msg_send (&my_ip, my_socket, ZMQ_SNDMORE);
zmq_msg_send (&my_message, my_socket, 0);

对于接收器,请参阅 zmq_msg_recv() 的文档.

For the receiver, see the documentation for zmq_msg_recv().

这篇关于有什么方法可以判断 ZeroMQ 消息来自哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆