确定 REQ-REP ZeroMQ 应用程序的客户端发出请求的端口,服务器绑定到两个端点(端口号)?

Identify the port on which request is made by client of REQ-REP ZeroMQ application,server is bound to two endpoints(port number)?

服务器绑定到端口#s 上的两个套接字:6666(为来自客户端的 SMS 发送请求提供服务)和 6661(为来自客户端的 EMAIL 发送请求提供服务)。

由于我想以不同的方式为它们提供服务,因此我必须首先知道请求来自哪个端口,以便我可以为该服务执行代码。

所以,我的问题是如何识别端口?

如果这不可能,那么可以应用什么逻辑来解决这个问题?

服务器端代码是:

int main () {

zmq::context_t context (1);               // Prepare our context and socket

zmq::socket_t socket  (context, ZMQ_REP); socket.bind  ("tcp://*:6666");
zmq::socket_t socket2 (context, ZMQ_REP); socket2.bind ("tcp://*:6661");

while (true) {      
  // ----------------------------------SMS CODE----------------------------------
     zmq::message_t request;
     socket.recv ( &request);             // Wait for next request from client

  /* ...                                  // SMS Send Logic
     ...
                                          */
     zmq::message_t reply (11);           // Send reply back to client
     memcpy (reply.data (), "SMS Details", 11);
     socket.send (reply);

  // --------------------------------EMAIL CODE----------------------------------
     zmq::message_t request2;
     socket2.recv (&request2);            // Wait for next request from client

  /* ...                                  // Email Send Logic
     ...                           
                                          */
     zmq::message_t reply2 (16);          // Send reply back to client
     memcpy (reply2.data (), "Email Details", 16);
     socket2.send (reply2);
  }                                       // end of while
return 0;
}

让我们揭开问题的神秘面纱并清理代码

代码主要是错误的,无法编译(检查那里的 un-matching curl-brace)。

接下来您的代码与您的文本说明不匹配(检查交换的 port-numbers 文本说 6666 用于电子邮件,而代码实现这个用于 SMS 处理)。

最后,即使是更正后的代码也不能很好地防止陷入相互的和主要的 un-salvageable 死锁(在 Whosebug 或其他地方阅读更多关于此 in many other posts be it here 的信息)。


然而,问题要求检测端口号。

鉴于在 .bind() 方法中分配的 port-numbers 已将这对 ZeroMQ-socket 实例分离地映射到不同的端口#-s,消息到达毫无疑问,socket-instance{ socket | socket2 }这样的消息已经到达

鉴于此,所有 SMS-designated 消息将来自端口 6666.

鉴于此,所有 MAIL-designated 消息都将来自端口 6661(除非发生了一些 sender-side 有意破坏,但这超出了问题的范围,不是吗?)

  • My question was how will we selectively run the SMS or EMAIL code depending on the port number on which the request comes.@user3666197 – Ayush Bajaj 32 mins ago?

否定,先生,您的问题是“如何识别端口?" - 即如何检测端口号,而您的代码在语法和语义设计上基本上都是错误的。

  • Syntax errors removed. I understand that the code enters deadlock. And that can be avoided by running the either SMS or EMAIL code depending on the port on which the request comes. – Ayush Bajaj 21 mins ago

Negative,先生,您的代码设计建议使用命令式纯 [SERIAL] 计划在第一次期望从 [读取时输入 blocking-wait-state =96=] access port#:6666 并且当且仅当这确实发生在未来的某个时间(永远不会保证确实发生),代码然后可能会进入下一个(再次阻塞)wait-state 并一直等待,直到一些(如果有的话)EMAIL-ingress 消息(希望)到达。

  • You wrote " there are no doubts upon a message arrival, from which socket-instance { socket | socket2 } such a message has arrived." How do you get to know on which port the request arrived (code please), so that accordingly we can serve it – Ayush Bajaj 17 mins ago

(请输入代码)?!?
您是否要求其他人完成这项工作并编写解决方案?

抱歉,这不是任何志愿站点。

如果您已将问题从“我如何识别端口?”转移到坚持使用 [=52] 等命令=](cit.:) “请输入代码”,请从 re-reading ZeroMQ [=102] 开始=] 有关使用 poll() 并正确完成工作的文档,无需要求他人。

Whosebug 社区热衷于帮助解决问题,这些问题已经制定了 MCVE-formulation 可行但不可行的问题,并且 MCVE-code-snippets 确实代表了一个人先前研究的有力证据和设计 + 调试工作,这显然不是 O/P 代码的情况,也不是进一步改变问题方向的情况。

永远不要问 "(请输入代码)"

你应该使用 zmq::poll to check which sockets have pending messages before attempting to read from them. There is an official cpp example here

您也可以使用 ZMQ_DONTWAIT 来检查消息,但您仍然应该轮询以避免使用过多 CPU。

示例:

#include "zeromq.hpp"

int main (int argc, char *argv[])
{
    zmq::context_t context(1);

    zmq::socket_t emailSocket  (context, ZMQ_REP); emailSocket.bind  ("tcp://*:6666");
    zmq::socket_t smsSocket (context, ZMQ_REP); smsSocket.bind ("tcp://*:6661");

    //  Initialize poll set
    zmq::pollitem_t items [] = {
        { emailSocket, 0, ZMQ_POLLIN, 0 },
        { smsSocket, 0, ZMQ_POLLIN, 0 }
    };
    //  Process messages from both sockets
    while (1) {
        zmq::message_t message;
        // wait until there is a message ready for one of the sockets
        zmq::poll (&items [0], 2, -1);

        // try to read/process a message from email socket (Don't wait, just skip if there are none)
        if (emailSocket.recv(&message, ZMQ_DONTWAIT)) {
            //  Process email request
            zmq::message_t response(...);
            emailSocket.send(&response);
        }
        // same again for sms socket
        if (smsSocket.recv(&message, ZMQ_DONTWAIT)) {
            //  Process sms request
            zmq::message_t response(...);
            smsSocket.send(&response);
        }
        // back to the top of the loop to wait for another message
    }
    return 0;
}