7

In C we have Sockets and descriptors, it is possible to just take one of these and hand them over to a Thread, this makes it possible that you can accept incoming connections and give the work to a Thread you like and the Thread can send by itself the response back.

My question is, how can I achieve this with ZeroMQ? With Request-Reply pattern it seems i cannot send and receive asynchronously, the responses have to be in sequence, my goal would be to have multiple clients to a single server, sending responses not in sequence.

I had a look at the Request Response pattern but the API clearly states that using that Socket with multiple Threads is a bad idea. Maybe i have missed something or ZeroMQ is more intelligent than i know of. If you need any further information just post a comment and i will do my best to give the information.

I had also a look at the provided examples: Code Examples

Here is the Socket description: ZMQ-Socket

Aleksandr Kravets
  • 5,750
  • 7
  • 53
  • 72
Oliver
  • 928
  • 9
  • 25

3 Answers3

3

After days of searching, a friendly person in IRC in the zeromq channel provided help for me.

An updated Link in case somebody is still looking for that. Should be persistant! http://zguide.zeromq.org/

This example really works well, its easy to adapt, i have used boost threads with a threadpool.

Oliver
  • 928
  • 9
  • 25
  • Unfortunately, that page doesn't exist. Care to post it here so this is an answer? – d-_-b Oct 16 '13 at 01:41
  • Hi d-_-b here is another option for it with waybackmachine http://web.archive.org/web/20120704201150/http://www.zeromq.org/blog:multithreaded-server – Oliver Oct 16 '13 at 10:54
3

Often, when you try to adapt existing designs to 0MQ, you get into trouble. Here the existing design passes sockets (typically, HTTP) to child processes or threads, and then allows them to respond. It's not a very elegant design. It works because each new connection exists as a socket, and because when a reply is sent the socket is destroyed. Neither of these apply to 0MQ applications.

The 0MQ design uses a frontend ROUTER socket that pulls in requests from clients. It then passes these requests to worker threads across inproc:// sockets, using a backend DEALER to deal out the requests. The worker threads use a REP socket to receive the requests, and send their replies back on. The main thread then polls on both frontend and backend socket, simply routing messages between the two.

Which is what that old blog article explains (using the legacy XREP/XREQ names), and it's explained in more detail with examples in many languages here: http://zguide.zeromq.org/page:all#Multithreading-with-MQ

Pieter Hintjens
  • 6,599
  • 1
  • 24
  • 29
1

As I understand You need a server which want to make new thread for each incoming connection and each thread reply to exactly one connection, if so this is sample code which exactly do this: http://www.kieser.net/linux/java_server.html

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Shahryar
  • 1,454
  • 2
  • 15
  • 32
  • 1
    Thanks Shahryar for your effort, my request here is really with "zeroMq" the reason for that is scaleability, i would like to be able to extend the application later to have workers on different machines to do the tasks, with zeromq i can "according to the model" expand my model with the library to have a different pattern and so have higher scaleability.. – Oliver Jun 28 '12 at 19:21