ZeroMQ 可以用于接受传统的套接字请求吗? [英] Can ZeroMQ be used to accept traditional socket requests?

查看:31
本文介绍了ZeroMQ 可以用于接受传统的套接字请求吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ZeroMQ 重写我们的一个旧服务器,现在我有以下服务器设置,(适用于 Zmq 请求):

I'm trying to re-write one of our old Servers using ZeroMQ, for now I have the following Server setup, (which works for Zmq requests):

    using (var context = ZmqContext.Create())
    using (var server = context.CreateSocket(SocketType.REP)) {
        server.Bind("tcp://x.x.x.x:5705");

        while (true) { ... }

如果我使用 Zmq 客户端库连接 context.CreateSocket(SocketType.REQ)

This kind of setup works fine if I use the Zmq client library to connect context.CreateSocket(SocketType.REQ)

但不幸的是,我们有很多遗留代码需要连接到此服务器,并且套接字是使用 .net 套接字库创建的:

But unfortunately we've got a lot of legacy code that needs to connect to this server and the sockets are created using .net socket libs:

    Socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
    Socket.Connect(ipAddress, port);

有没有办法编写一个 ZeroMQ 服务器来接受这些传统的 .net 套接字连接?

Is there a way to write a ZeroMQ Server to accept these traditional .net socket connections?

推荐答案

您可以使用 ZMQ_STREAM 套接字实现此目的.

请注意,从 zeroMQ 4.x 开始,RAW 路由器选项已被弃用一种新的 ZMQ_STREAM 套接字类型,其工作方式与 ROUTER + RAW 相同.

Please note that since zeroMQ 4.x, the RAW router option has been deprecated for a new ZMQ_STREAM socket type, that works the same way as ROUTER + RAW.

不过,它似乎必然会发展.

It seems it is bound to evolve, though.

我最近在 4.0.1 版本中尝试了 ZMQ_STREAM 套接字.

I recently tried ZMQ_STREAM sockets in version 4.0.1.

你可以打开一个,使用zmq_rcv直到你收到完整的消息(你必须自己检查它是完整的),或者zmq_msg_rcv让ZeroMQ处理它.您将收到一个 identifier 消息部分,就像您在 ROUTER 套接字中找到的 identifier 一样,后面紧跟一个 ONLY body 部分.它们之间没有空分隔符,就像使用 REQ 套接字与 ROUTER 套接字通信一样.所以如果你路由它们,一定要自己添加.

You can open one, use zmq_rcv until you receive the whole message (you have to check it is whole yourself), or zmq_msg_rcv to let ZeroMQ handle it. You will receive an identifier message part, just like the identifier you would find in ROUTER sockets, directly followed by one ONLY body part. There is no empty delimiter between them like there would be using a REQ Socket talking to a ROUTER Socket. So if you route them, be sure to add it yourself.

但请注意:如果另一端存在延迟,或者您的消息超过 ZeroMQ ZMQ_STREAM 缓冲区(我的是 8192 字节长),您的消息可以被 zeroMQ 解释为一系列消息.

Beware though: if there is latency on the other end or if your message exceeds ZeroMQ ZMQ_STREAM buffers (mine are 8192 bytes long), your message can be interpreted by zeroMQ as a series of messages.

在这种情况下,您将收到许多不同的 ZeroMQ 消息,包括标识符部分正文部分,并且聚合它们是您的工作,知道如果多个客户端正在与 STREAM 套接字通信,它们可能会混淆.我个人使用以二进制标识符为键的哈希表,当我知道消息完成并发送到下一个节点时,从表中删除条目.

In that case, you will receive as many different ZeroMQ messages including both the identifier part and the body part, and it is your job to aggregate them, knowing that if several clients are talking to the STREAM socket, they might get mixed up. I personnally use a hash table using the binary identifier as a key, and delete the entry from the table when I know the message is complete and sent to the next node.

通过带有 zmq_msg_sendzmq_sendZMQ_STREAM 发送可以正常工作.

Sending through a ZMQ_STREAM with zmq_msg_send or zmq_send works fine as is.

这篇关于ZeroMQ 可以用于接受传统的套接字请求吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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