1

I have this implementation to create a socket :

if (gctx == nullptr)
    {
        gctx = zmq_ctx_new();
        gsock = zmq_socket(gctx, ZMQ_REQ);
    }

    snprintf(url, sizeof(url), "wsd:///tmp/hfg/%s", name);
    int rc = zmq_connect(gsock, url);
    if (rc != 0)
        printf("error connect %s: %s\n", url, zmq_strerror(zmq_errno()));

    return rc;

But I want to be able to create multiple sockets, not just one. How is this done? Do I also need multiple contexts? I mean for every socket a context.

user3666197
  • 1
  • 6
  • 50
  • 92
  • Did you read the [documentation of 0mq](https://zeromq.org/get-started/)? Please provide some [mre] in your question and cite documentation. Notice that ZeroMQ is opensource, so study its source code – Basile Starynkevitch Jun 15 '20 at 09:06

1 Answers1

0

Do I also need multiple contexts?

No, you need not.

How is this done?

gSock1 = zmq_socket( gCTX, ZMQ_REQ ); // 1st REQ-uester
gSock2 = zmq_socket( gCTX, ZMQ_REQ ); // 2nd
gSock3 = zmq_socket( gCTX, ZMQ_PUB ); // 1st PUB-lisher
gSock4 = zmq_socket( gCTX, ZMQ_PUB ); // 1st PUB-lisher

As simple as assigning as many instances of Socket()-class ( or zmq_socket() calls ) as needed. The default Context()-instance, the main message-passing processing engine, may remain "shared" or one may increase it's number of IO-threads, if needed and/or fine tune it's other configuration details as needed or even split the processing workloads among several Context()-instances if needed.

halfer
  • 19,824
  • 17
  • 99
  • 186
user3666197
  • 1
  • 6
  • 50
  • 92