1

I have the following server (C++):

#include <nng/nng.h>
#include <nng/protocol/pubsub0/pub.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <unistd.h>
void
fatal(const char *func, int rv)
{
        fprintf(stderr, "%s: %s\n", func, nng_strerror(rv));
}
int main(int argc, char* argv[]){
    nng_socket sock;
    int rv;
    std::string url = "tcp://0.0.0.0:" + std::to_string(5563);
    if ((rv = nng_pub0_open(&sock)) != 0) {
            fatal("nng_pub0_open", rv);
    }
    if ((rv = nng_listen(sock, url.c_str(), NULL, 0)) < 0) {
            fatal("nng_listen", rv);
    }
    while(1){
        std::string msg = std::string("msg");
        //if ((rv = nng_send(sock, (void*)frame, bpp * nImgW * nImgH, 0)) != 0) {
        if ((rv = nng_send(sock, (void*)msg.c_str(), 3, 0)) != 0) {
            fatal("nng_send", rv);
        }else{
            std::cout << "Frame Sent... "<< std::endl;
        } 
        sleep(1);
    }
}

And the following client (python):

import pynng
from pynng import Pub0, Sub0, Timeout
cam_path = "tcp://127.0.0.1:5563"
with Sub0(dial=cam_path,recv_timeout=2000, topics=b'') as sub:
    sub.recv_max_size = 0 #recieve msg of any size
    while(1):
        try:
            print("waiting for msg")
            img = sub.recv()
            print(img)
        except pynng.exceptions.Timeout as e:
            print('Timed out, retrying...')

I dont understand why no messages ever arrive to the client. I have set topics and recv_max_size but still no messages arrives at the client. What am I doing wrong here now?

Michael Hansen
  • 237
  • 2
  • 8
  • I found out what the issue is. It was because another nng publisher was running on that same port. Why is there not an error thrown when trying to bind to an address/port which is occupied? – Michael Hansen May 12 '20 at 14:57
  • 1
    I think you just need to change your check from `nng_listen(...) < 0` to `nng_listen(...) != 0`. `nng_listen` does return an error code whenever it cannot bind. – Cody Piersall May 12 '20 at 19:46

1 Answers1

0

Q : "What am I doing wrong here now?"

You happened to blindly assume that things happen in a very way your code does not attempt to validate. Old assembler wolves used to state # ASSUME NOTHING before they ever started a code :o)

Pieter Hintjens', the famous AMQP / ZeroMQ evangelisation Guru (an older brother of the nanomsg / nng ) uses explicit POSACK-s from assert-s :

if ( (  aRetCODE = nng_pub0_open( &sock ) ) != 0 ) {
        fatal( "nng_pub0_open",
                aRetCODE );
}
assert( aRetCODE == 0 && "EXC: Pub0 socket failed to instantiate" );

if ( (  aRetCODE = nng_listen( sock, url.c_str(), NULL, 0 ) ) < 0 ) {
        fatal( "nng_listen",
                aRetCODE );
}
assert( aRetCODE == 0 && "EXC: Pub0 socket failed to .listen()" );

Python has also a similar assert-tool for explicit POSACK-checks, so worth polishing the code-robustness using them as Pieter Hintjens has made Martin Sustrik to do so wherever you read the API :o)

assert rc == 0, "INF: some condition was not met for ( %r )" % someVariableNAME

Happy using nng / pynng for your Projects!

user3666197
  • 1
  • 6
  • 50
  • 92