zeromq / czmq

High-level C binding for ØMQ
czmq.zeromq.org
Mozilla Public License 2.0
1.16k stars 523 forks source link

How to create a SUB socket and Bind? No Bind/Connect methods available #2224

Closed SpaceMonkeyForever closed 2 years ago

SpaceMonkeyForever commented 2 years ago

In ZMQ, you can subscribe and bind (instead of connect) to a URL so that anyone can publish to you.

Here is how to do it in Python:

    context = zmq.Context()
    socket = context.socket(zmq.SUB)
    socket.setsockopt(zmq.SUBSCRIBE, 'Child:')
    # Even though I'm the subscriber, I'm allowed to `bind`
    socket.bind('tcp://127.0.0.1:5000')

I'm trying to do that using the Java bindings, but there is no "connect" or "bind". A subscriber socket is created using Zsock.newSub:

    public static Zsock newSub(String endpoint, String subscribe) {
        return new Zsock(__newSub(endpoint, subscribe));
    }

How do I bind using CZMQ?

sphaero commented 2 years ago

I've not used Java but in C you would:

zsock_t *sub_socket = zsock_new(ZMQ_SUB);
rc = zsock_bind(sub_socket, "tcp://127.0.0.1:1234");
zsock_set_subscribe(sub_socket, "");

Should be the same in Java.

I know there are convenience methods that do this for you but I generally not use them.

SpaceMonkeyForever commented 2 years ago

The problem is that in Java, there is no equivalent to "zsock_bind".

SpaceMonkeyForever commented 2 years ago

very strange.... I've just found the "bind" method. I don't know why I couldn't see it before. Checking