问题描述
我希望服务器向客户端发送一些消息,然后客户端对其进行确认.我被分配了这项任务.我可以在C linux中使用单个消息队列来完成此操作,还是需要创建两个消息队列?
I want server to send some message to client and client to ACK it. I have been given this assignment. Can I do it using a single message queue in C linux or I need to create two??
谢谢:)
推荐答案
是的,可以使用sysV消息队列来完成此任务,而sysV消息队列可以通过查看您先前使用的问题来解决.您可以使用消息格式中嵌入的msgtype
字段来指定消息的类型,并且适当的接收过程必须在其msgrcv
调用中指定该msgtype并处理该类型的消息.
Yes, it is possible to do this with sysV messages queues, which from looking at your previous questions, you are using. You can use the msgtype
field embedded in the message format to specify what kind of message it is and the appropriate receiving process has to specify that msgtype in its msgrcv
call and process messages of that type.
因此,例如,服务器可以写入msgtype = 1,而客户端可以确认msgtype2.
So, for example, server can write msgtype = 1 and client can ack msgtype 2.
请注意,这需要您认真考虑您的消息传递方案-仔细阅读msgrcv
文档,以便您了解如何阅读消息的选项-以及执行此操作的原因.如果您做的不正确,将无法很好地扩展-在性能上,而不是在编程复杂性上-并且很容易使自己陷入程序追尾的情况.
Note that this requires you to really think out your messaging scheme - read the msgrcv
docs carefully so you understand the options for how messages can be read - and why you are doing it. If you don't do it right it won't scale well - not in performance but in programming complexity - and it will be easy to get yourself in a situation where your programs are chasing their own tails.
这是否比仅使用两个队列更聪明,我将让您决定.
Whether this is smarter than just using two queues I will leave for you to decide.
请注意,您实际上无法使用POSIX消息队列执行此操作.
Note that you really can't do this with POSIX message queues.
这篇关于使用C中的单个消息队列是否可以进行两种方式的通讯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!