简体   繁体   English

ZeroMQ服务器/客户端类型的正确模式

[英]Correct pattern for ZeroMQ Server/Client type

I have a server that needs to get instructions to run processes for clients on another machine. 我有一台服务器,该服务器需要获取有关在另一台计算机上为客户端运行进程的指令。

The clients send a job message, the Server processes the job and later sends the back results. 客户端发送作业消息,服务器处理作业,然后发送回结果。

I tried using the NetMQ Request-Response pattern (see below) 我尝试使用NetMQ请求-响应模式(请参见下文)

This works nicely for 1 client, BUT if a second client sends a request before previous client job is finished - I get an error. 这对于1个客户端来说效果很好,但如果第二个客户端在上一个客户端作业完成之前发送了请求,则会出错-我收到错误消息。

I really need to be able to receive ad-hoc messages from clients, and send results when they are completed. 我确实需要能够接收来自客户端的临时消息,并在完成后发送结果。 Clearly, I am using the wrong pattern, but reading the ZeroMQ docs has not highlighted a more appropriate one. 显然,我使用了错误的模式,但是阅读ZeroMQ文档并没有突出显示一个更合适的文档。

namespace Utils.ServerMQ
{
    class ServerMQ
    {
        public static void Go()
        {
            using (var responseSocket = new ResponseSocket("@tcp://*:393"))
            {
                while (true)
                {
                    Console.WriteLine("Server waiting");
                    var message = responseSocket.ReceiveFrameString();

                    Console.WriteLine("Server Received '{0}'", message);

                    //System.Threading.Thread.Sleep(1000);
                    var t2 = Task.Factory.StartNew(() =>
                    {
                        RunProcMatrix(message, responseSocket);
                    });

                }
            }
        }

        public static void RunProcMatrix(object state, ResponseSocket responseSocket)
        {
            var process = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = Path.Combine(@"H:\Projects\Matrix\Matrix\bin\Debug\", "Matrix001.exe"),
                    Arguments = (string)state,
                    WindowStyle = ProcessWindowStyle.Normal,
                    CreateNoWindow = false
                }
            };
            process.Start();
            process.WaitForExit();


            responseSocket.SendFrame((string)state);
        }
    }
}

You want a ROUTER socket on the server side, so it can receive multiple requests at a time. 您需要在服务器端使用ROUTER套接字,以便它一次可以接收多个请求。 ( Guide ) REQ sockets on the client side are still fine unless the server may arbitrarily push data to them, then they need to be DEALER sockets. 指南 )除非服务器可以将数据任意推送到客户端,否则客户端的REQ套接字仍然可以使用,因此它们必须是DEALER套接字。

Note that for sockets beyond REQ/RESP you need to manually handle the message envelope (the first frame of the message indicating its destination). 请注意,对于超出REQ / RESP的套接字,您需要手动处理消息信封(消息的第一帧指示其目的地)。 Guide 指南

The 0MQ docs are incredibly dense... I don't blame you for not intuiting this from them :) 0MQ文档非常密集...我不怪你没有从他们那里领悟到这一点:)

This example from the NetMQ docs is full ROUTER-DEALER: https://netmq.readthedocs.io/en/latest/router-dealer/#router-dealer , you can take just the router side and it should work the same though. NetMQ文档中的此示例是完整的ROUTER-DEALER: https ://netmq.readthedocs.io/zh-CN/latest/router-dealer/#router-dealer,您可以只使用路由器端,但是应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ZeroMQ模式用于1到N,在客户端重试并为服务器连接知识 - ZeroMQ Pattern to use for 1 to N with retry on client and connect knowledge for server 使用ZeroMQ通过Internet进行客户端/服务器通信 - Client/Server communication over the internet using ZeroMQ 设计模式-客户端服务器-命令模式 - Design pattern - Client Server - Command pattern ZeroMQ,客户端< - >服务器,只有让客户端连接到主机才能实现双向通信? - ZeroMQ, Client<-> Server , bi-directional communication possible with only having the client connect to host? REQ / REP模式中的ZeroMQ FiniteStateMachineException - ZeroMQ FiniteStateMachineException in REQ/REP pattern ZeroMQ C#客户端不接收来自C ++服务器的消息 - ZeroMQ C# client doesnt receive messages from C++ server ZeroMQ C#客户端未接收c ++服务器发布/订阅版本3.2 - ZeroMQ C# client not receiving c++ Server Publish/Subscribe Versions 3.2 如何使用 ZeroMQ 从 C# 客户端向 C++ 服务器发送消息 - How to send a message from C# client to C++ server using ZeroMQ 用于在服务器和客户端之间实现心跳的模式 - Pattern to implement heartbeat between server and the client Command模式或Strategy模式更适合客户端 - 服务器调用吗? - Is the Command pattern or the Strategy pattern more appropriate for client-server calls?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM