简体   繁体   English

使用C#和python的NetMQ扩展请求-答复

[英]NetMQ Extended Request-Reply using c# and python

I am able to have c# (client) and python (server) talk to each other by using a simple request-reply. 通过使用简单的请求-回复,我能够让c#(客户端)和python(服务器)互相交谈。 However, I want my web application built on c# asp.net to be stable and need more clients and servers, so I tried connecting c# and python using the Extended REQ-REP connection. 但是,我希望基于c#asp.net的Web应用程序稳定并需要更多客户端和服务器,因此我尝试使用扩展REQ-REP连接来连接c#和python。

But when I run the code below, it does not do its job as a broker and outputs nothing. 但是,当我运行下面的代码时,它不会充当代理,也不会输出任何内容。 What am I doing wrong here? 我在这里做错了什么?

5600 = c# client 5600 = C#客户端

5601 = python server 5601 = python服务器

using (var frontend = new RouterSocket("@tcp://127.0.0.1:5600"))
                        using (var backend = new DealerSocket("@tcp://127.0.0.1:5601"))
                        {
                            // Handler for messages coming in to the frontend
                            frontend.ReceiveReady += (s, p) =>
                            {
                                var msg = p.Socket.ReceiveFrameString();
                                backend.SendFrame(msg); // Relay this message to the backend
                            };

                            // Handler for messages coming in to the backend
                            backend.ReceiveReady += (s, p) =>
                            {
                                var msg = p.Socket.ReceiveFrameString();
                                frontend.SendFrame(msg); // Relay this message to the frontend
                            };

                            using (var poller = new NetMQPoller { backend, frontend })
                            {
                                // Listen out for events on both sockets and raise events when messages come in
                                poller.Run();
                            }
                        }

You are not sending the all message frames with the correct flags. 您没有发送带有正确标志的所有消息帧。

You can try and user Proxy of netmq that does exactly that. 您可以尝试使用netmq的用户Proxy来实现。 If you still want to write it by hand take a look how the proxy do it with the correct frames flags here: 如果您仍然想手工编写它,请看一下代理如何使用正确的帧标志来完成它:

https://github.com/zeromq/netmq/blob/master/src/NetMQ/Proxy.cs https://github.com/zeromq/netmq/blob/master/src/NetMQ/Proxy.cs

Update: 更新:

Following is an example on how to use the proxy in your case: 以下是有关如何在您的情况下使用代理的示例:

using (var frontend = new RouterSocket("@tcp://127.0.0.1:5600"))
using (var backend = new DealerSocket("@tcp://127.0.0.1:5601"))
{
    using (var poller = new NetMQPoller { backend, frontend })
    {
        var proxy = new Proxy(frontend, backend, null, poller);
        proxy.Start();
        proxy.Run();
    }
}

You can also use it without a poller: 您也可以在没有轮询器的情况下使用它:

using (var frontend = new RouterSocket("@tcp://127.0.0.1:5600"))
using (var backend = new DealerSocket("@tcp://127.0.0.1:5601"))
{       
    var proxy = new Proxy(frontend, backend);
    proxy.Start();        
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM