0

I am writing a client / server application that needs to work with IPC. I have developed the program using TCPSockets but now would like to attempt using message queues. The server just runs until a client connects. After the client connects it can send the server any integer, which is then displayed on the server. Multiple clients can connect to the server and send it integers, which is visible to the server and connected clients.

Without using sockets, how would I approach implementing this? There can be any number of clients connecting. I was thinking of using a QThread to handle polling to see if a client is connected(?) but this is where I am stuck -- how do you tell a client is connected?

Amar Daadaa
  • 73
  • 1
  • 1
  • 4

2 Answers2

1

See this question I asked regarding establishing a "connection" using POSIX message queues: Using POSIX message queues instead of TCP sockets - how to establish "connection"?

I did get it working, but ultimately developed support for Unix domain sockets, which gave the speedup I enjoyed with MQ but without the implementation complexity. Now, for some reason you said in your question "without using sockets" so I don't know if you'll be amenable to using Unix domain sockets to replace TCP instead of using MQ, but if you are willing to try, I do recommend it. Unix domain sockets do support connection-oriented operation (as well as connectionless), so your issues of how to know when a connection is made or broken are addressed without application complexity (the same as with TCP).

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
0

This is not a particularly good use case for MQs but you can do it.

There is no real notion of "connection" with message queues. Conceptually they are more like a mailboxes. Your client drops a letter in and sometime later - milliseconds to days depending on how the server is designed - the server decides to open the message and process it.

In your case you might consider having a known queue (clients know the path to the MQ) that the clients can send messages to. Each client will create a queue of their own on which to receive responses. The message to the server could contain the path to a queue that the clients will read their responses from. The server responds with the integer to the client's queue. This will work but it's not something you want to scale to any degree.

Both client and server have a couple of options for reading their queues. They can block in mq_receive until there is something to read. They can poll using mq_timedreceive. They can use mq_notify to receive a signal (or create a thread) when there is something new in the queue. Or, on linux, they can use the mqd_t in a select (or similar) statement.

Duck
  • 26,924
  • 5
  • 64
  • 92