[zeromq-dev] Only receive a single published message per connection?

Michael Small michaelsmall at me.com
Thu Aug 5 07:29:16 CEST 2010


So debugging this further I seem to be seeing a lot of "Resource temporarily unavailable" errors.
Using this codebase:

struct Pub {
    zmq::socket_t s;
    Pub(zmq::context_t& ctx)
        : s(ctx, ZMQ_PUB) {}
    void exec() {
        const char* str = "topic|This is a test";
        std::size_t sz = strlen(str);
        try {
        while(true) {
            zmq::message_t msg(sz);
            memcpy(msg.data(), str, sz-1);
            int r = s.send(msg);
            if(r != 0) {
                DEBUG(INFO, ("[pub] r = %d, err=%s, errno=%d", r, strerror(errno), errno));
                errno = 0;
            }
            sleep(2);
        }
        } catch(std::exception& e) {
            DEBUG(ERROR, ("[pub] exception: %s", e.what()));
        }
    }
};

struct Sub {
    zmq::socket_t s;
    Sub(zmq::context_t& ctx)
        : s(ctx, ZMQ_SUB) {}
    void exec() {
        try {
        while(true) {
            zmq::message_t msg;
            int r = s.recv(&msg);
            if(r != 0) {
                DEBUG(INFO, ("[sub] r = %d, err=%s, errno=%d", r, strerror(errno), errno));
                break;
            } else {
                const char* str = reinterpret_cast<const char*>(msg.data());
                printf("Received: %s\n", str);
            }
        }
        } catch(std::exception& e) {
            DEBUG(ERROR, ("[sub] exception: %s", e.what()));
        }
    }
};

int main(int argc, char** argv) {
    (void)argc;
    zmq::context_t *ctx = new zmq::context_t(1);
    Pub pub(*ctx);
    Sub sub(*ctx);
    pub.s.bind(argv[1]);
    sub.s.connect(argv[1]);
    sub.s.setsockopt(ZMQ_SUBSCRIBE, "", 1);
    boost::thread t_pub(boost::bind(&Pub::exec, &pub));
    boost::thread t_sub(boost::bind(&Sub::exec, &sub));
    getchar();
    delete ctx;
    t_pub.join();
    t_sub.join();
    return 0;
}

On 05/08/2010, at 2:54 PM, Michael Small wrote:

> zmq::message_t msg(sz);
> uint8_t* bytes = reinterpret_cast<uint8_t*>(msg.data());
> memcpy(&bytes[0], topic.c_str(), topic_sz); 
> memcpy(&bytes[topic_sz], &t, obj_sz);
> int r = s.send(msg, 0);
> 
> ...
> s is a connected ZMQ_PUB socket. 
> 
> 
> On 04/08/2010, at 10:38 PM, Matt Weinstein wrote:
> 
>> Send returns 0 on success ( for me )
>> 
>> Got a code sample?
>> 
>> 
>> 
>> On Aug 4, 2010, at 4:06 AM, Michael Small <michaelsmall at me.com> wrote:
>> 
>>> That sounds like a reasonable assumption. I think it's time to delve into the source and have a look at how all this works.
>>> 
>>> Thanks guys
>>> 
>>> On 04/08/2010, at 5:37 PM, Pieter Hintjens wrote:
>>> 
>>>> The message is set to empty when sent, so the second call presumably succeeds but sends an empty message.
>>>> 
>>>> -Pieter
>>>> On Aug 4, 2010 6:02 AM, "Michael Small" <michaelsmall at me.com> wrote:
>>>> > Ah, I see.
>>>> > 
>>>> > So creating another message entirely does the trick and I'm seeing multiple messages received now.
>>>> > However it leaves me a bit curious because the result of s.send(msg) in both cases was 1.
>>>> > 
>>>> > [m] s.send(msg_1): 1, data: fsm.t|This is a test (fsmd.cpp:50 in main) I
>>>> > [m] s.send(msg_2): 1, data: fsm.p|This is a test (fsmd.cpp:52 in main) I
>>>> > [m] s.send(msg_3): 1, data: fsm.t|This is a test (fsmd.cpp:54 in main) I
>>>> > 
>>>> > According to the manual "The zmq_send() function shall return zero if successful. Otherwise it shall return -1" so the manual is incorrect perhaps?
>>>> > 
>>>> > On 04/08/2010, at 1:48 PM, Chuck Remes wrote:
>>>> > 
>>>> >> On Aug 3, 2010, at 10:33 PM, Michael Small wrote:
>>>> >> 
>>>> >>> I've observed strange behaviour in my initial testing of pub/sub.
>>>> >>> 
>>>> >>> The relevant subscriber code:
>>>> >>> 
>>>> >>> while(s.recv(&msg)) {
>>>> >>> printf("Received: %s\n", (char*)msg.data());
>>>> >>> }
>>>> >>> 
>>>> >>> And the publish code:
>>>> >>> 
>>>> >>> s.send(msg);
>>>> >>> s.send(msg);
>>>> >>> getchar();
>>>> >>> 
>>>> >>> The first message is received, but the second is not. If I kill the publisher and restart (leaving the subscriber active), again the first message is received, but not the second.
>>>> >>> Is there some semantic of this pattern that I'm not aware of?
>>>> >>> 
>>>> >>> This is being tested on an OS X 10.6.4 box.
>>>> >> 
>>>> >> As soon as you hand off a message to 0mq, it becomes the owner of the message lifecycle. Under the covers that "msg" is released, so the second send is very likely not succeeding due to the message no longer existing. You should check the return codes of each send to be sure.
>>>> >> 
>>>> >> cr
>>>> >> 
>>>> >> _______________________________________________
>>>> >> zeromq-dev mailing list
>>>> >> zeromq-dev at lists.zeromq.org
>>>> >> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>>>> > 
>>>> 
>>>> _______________________________________________
>>>> zeromq-dev mailing list
>>>> zeromq-dev at lists.zeromq.org
>>>> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>>> 
>>> _______________________________________________
>>> zeromq-dev mailing list
>>> zeromq-dev at lists.zeromq.org
>>> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>> _______________________________________________
>> zeromq-dev mailing list
>> zeromq-dev at lists.zeromq.org
>> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
> 
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20100805/7cd62e13/attachment.htm>


More information about the zeromq-dev mailing list