It is a good library. IMHO after some my small research, NNG better ZeroMQ (TLS, websockets, thread-safe, “unkillable” req-rep, etc)
See RATIONALE
/* * Author, Copyright: Oleg Borodin <onborodin@gmail.com> */ #include <stdlib.h> #include <stdio.h> #include <string.h> #include <time.h> #include <unistd.h> #include <nng/nng.h> #include <nng/protocol/pubsub0/pub.h> #include <nng/protocol/pubsub0/sub.h> char* date(void) { time_t now = time(&now); struct tm *info = localtime(&now); char *text = asctime(info); text[strlen(text)-1] = '\0'; return text; } 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; char url[] = "tcp://127.0.0.1:1024"; if ((rv = nng_pub0_open(&sock)) != 0) { fatal("nng_pub0_open", rv); } if ((rv = nng_listen(sock, url, NULL, 0)) < 0) { fatal("nng_listen", rv); } while(1) { char* ts = date(); printf("nng send %s\n", ts); if ((rv = nng_send(sock, ts, strlen(ts) + 1, 0)) != 0) { fatal("nng_send", rv); } sleep(1); } }
/* * Author, Copyright: Oleg Borodin <onborodin@gmail.com> */ #include <stdlib.h> #include <stdio.h> #include <string.h> #include <time.h> #include <unistd.h> #include <nng/nng.h> #include <nng/protocol/pubsub0/pub.h> #include <nng/protocol/pubsub0/sub.h> void fatal(const char *func, int rv) { fprintf(stderr, "%s: %s\n", func, nng_strerror(rv)); } int main(int argc, char **argv) { char name[] = "client01"; nng_socket sock; int rv; char url[] = "tcp://127.0.0.1:1024"; if ((rv = nng_sub0_open(&sock)) != 0) { fatal("nng_sub0_open", rv); } if ((rv = nng_setopt(sock, NNG_OPT_SUB_SUBSCRIBE, "", 0)) != 0) { fatal("nng_setopt", rv); } if ((rv = nng_dial(sock, url, NULL, 0)) != 0) { fatal("nng_dial", rv); } while(1) { char *buffer = NULL; size_t size; if ((rv = nng_recv(sock, &buffer, &size, NNG_FLAG_ALLOC)) != 0) { fatal("nng_recv", rv); } printf("client (%s) received: %s\n", name, buffer); nng_free(buffer, size); } }
/* * Author, Copyright: Oleg Borodin <onborodin@gmail.com> */ #include <stdlib.h> #include <stdio.h> #include <string.h> #include <time.h> #include <unistd.h> #include <nng/nng.h> #include <nng/protocol/reqrep0/rep.h> #include <nng/protocol/reqrep0/req.h> #include <nng/protocol/pubsub0/pub.h> #include <nng/protocol/pubsub0/sub.h> char* date(void) { time_t now = time(&now); struct tm *info = localtime(&now); char *text = asctime(info); text[strlen(text)-1] = '\0'; return text; } 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; char url[] = "tcp://127.0.0.1:1024"; if ((rv = nng_rep0_open(&sock)) != 0) { fatal("nng_rep0_open", rv); } if ((rv = nng_listen(sock, url, NULL, 0)) != 0) { fatal("nng_listen", rv); } while(1) { char *buf = NULL; size_t sz; if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) != 0) { fatal("nng_recv", rv); } printf("received request\n"); char *d = date(); printf("sending %s\n", d); if ((rv = nng_send(sock, d, strlen(d) + 1, 0)) != 0) { fatal("nng_send", rv); } nng_free(buf, sz); } }
/* * Author, Copyright: Oleg Borodin <onborodin@gmail.com> */ #include <stdlib.h> #include <stdio.h> #include <string.h> #include <time.h> #include <unistd.h> #include <nng/nng.h> #include <nng/protocol/reqrep0/rep.h> #include <nng/protocol/reqrep0/req.h> #include <nng/protocol/pubsub0/pub.h> #include <nng/protocol/pubsub0/sub.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; char url[] = "tcp://127.0.0.1:1024"; if ((rv = nng_req0_open(&sock)) != 0) { fatal("nng_socket", rv); } if ((rv = nng_dial(sock, url, NULL, 0)) != 0) { fatal("nng_dial", rv); } while(1) { char *buf = NULL; char data[] = "date"; size_t sz = 0; printf("sending request\n"); if ((rv = nng_send(sock, data, strlen(data), 0)) != 0) { fatal("nng_send", rv); } if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) != 0) { fatal("nng_recv", rv); } printf("received date %s\n", buf); nng_free(buf, sz); sleep(1); } nng_close(sock); return (0); }