使用POSIX消息队列而不是TCP套接字 - 如何建立“连接”? [英] Using POSIX message queues instead of TCP sockets - how to establish "connection"?

查看:257
本文介绍了使用POSIX消息队列而不是TCP套接字 - 如何建立“连接”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有客户端和服务器程序,现在通过TCP进行通信。我试图使用POSIX消息队列(当客户端和服务器在同一台机器上,当然)。我的希望是,它会提高性能(特别是通过减少延迟)。

I have client and server programs which now communicate via TCP. I'm trying out using POSIX message queues instead (in cases where the client and server are on the same machine, of course). My hope is that it will improve performance (specifically via reduced latency).

我已经解决了大部分,但不知道一件事:如何建立连接。服务器同时接受来自多个客户端的连接,所以我想模拟TCP连接建立过程,如下所示:

I've worked out most of it, but am not sure about one thing: how to establish the "connection." The server accepts connections from multiple clients concurrently, so I'm tempted to emulate the TCP connection establish process like so:


  1. 服务器打开一个队列使用一个着名的名称并连续读取它(它可以使用 select(2)和TCP一样)。

  2. 客户端打开三个队列:两个具有任意名称(包括一些唯一性,例如PID以避免冲突)和一个具有服务器使用的知名名称。

  3. 客户端发送连接消息

  4. 服务器打开客户端连接消息中指定的队列(包括客户端的队列名称)并开始从客户端到服务器端读取(选择)。

  5. 客户端使用知名名称关闭服务器队列。双向通信使用客户端命名的两个队列(每个方向一个队列)。

  1. Server opens a queue with a well-known name and reads from it continuously (it can use select(2) as with TCP).
  2. Client opens three queues: two with arbitrary names (including some uniqueness such as PID to avoid collisions), and one with the well-known name used by the server.
  3. Client posts a "connect" message to the server's queue, including the client's queue names (one is designated for client-to-server traffic and the other for the converse).
  4. Server opens the queues named in the client's connect message and begins to read (select) from the client-to-server one.
  5. Client closes the server queue with the well-known name. Two-way communication proceeds using the two queues named by the client (one for each direction).

你可以看到这个方案类似于普通的TCP方法,这不是意外。但是,我想知道:

You can probably see how this scheme is similar to the common TCP method, and that's no accident. However, I'd like to know:


  1. 你能想出更好的方法吗?

  2. 您是否看到我的方法存在任何潜在问题?

  3. 您有任何其他想法,包括在同一台计算机上使用消息队列而不是TCP可能会提高性能(延迟)?

请记住,我以前没有使用POSIX消息队列,但是这是不同的)。平台是Linux。

Keep in mind that I haven't used POSIX message queues before (I did use IBM WebSphere MQ a while back, but that's rather different). The platform is Linux.

推荐答案

我最终实现它基本上如我所述,并有一些增强:

I ended up implementing it basically as I described, with a few enhancements:


  • 在步骤2中,我使用GUID作为队列名称,而不是合并客户端的PID。

  • 我添加了从服务器向客户端发送接受消息。

  • 当任一方希望结束通信时,将发送断开连接消息。

握手比TCP简单,但似乎已经足够了。

The handshaking is simpler than TCP, but seems sufficient.

对于延迟:使用POSIX消息队列而不是TCP在同一台机器上的大约75%的延迟减少。我的邮件大小为100个字节。

As for latency: it's much better. Roughly 75% less latency using POSIX message queues instead of TCP on the same machine. My messages are on the order of 100 bytes each.

这篇关于使用POSIX消息队列而不是TCP套接字 - 如何建立“连接”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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