{"id":205,"date":"2016-07-04T23:10:18","date_gmt":"2016-07-04T22:10:18","guid":{"rendered":"https:\/\/solidt.eu\/blog\/?p=205"},"modified":"2016-07-04T23:10:18","modified_gmt":"2016-07-04T22:10:18","slug":"zeromq-c-example","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/zeromq-c-example\/","title":{"rendered":"ZeroMQ C Example"},"content":{"rendered":"<pre lang=\"c\" class=\"\">#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n\r\n\r\nint main (void)\r\n{\r\n\/\/ Prepare our context and sockets\r\n    void *context = zmq_ctx_new ();\r\n    void *frontend = zmq_socket (context, ZMQ_ROUTER);\r\n    void *backend = zmq_socket (context, ZMQ_DEALER);\r\n    zmq_bind (frontend, \"tcp:\/\/*:5559\");\r\n    zmq_bind (backend, \"tcp:\/\/*:5560\");\r\n\r\n\/\/ Initialize poll set\r\n    zmq_pollitem_t items [] =\r\n    {\r\n        { frontend, 0, ZMQ_POLLIN, 0 },\r\n        { backend, 0, ZMQ_POLLIN, 0 }\r\n    };\r\n\/\/ Switch messages between sockets\r\n    while (1)\r\n    {\r\n        zmq_msg_t message;\r\n        zmq_poll (items, 2, -1);\r\n        if (items [0].revents &amp; ZMQ_POLLIN)\r\n        {\r\n            while (1)\r\n            {\r\n\/\/ Process all parts of the message\r\n                zmq_msg_init (&amp;message);\r\n                zmq_msg_recv (&amp;message, frontend, 0);\r\n                int more = zmq_msg_more (&amp;message);\r\n                zmq_msg_send (&amp;message, backend, more? ZMQ_SNDMORE: 0);\r\n                zmq_msg_close (&amp;message);\r\n                if (!more)\r\n                    break; \/\/ Last message part\r\n            }\r\n        }\r\n        if (items [1].revents &amp; ZMQ_POLLIN)\r\n        {\r\n            while (1)\r\n            {\r\n\/\/ Process all parts of the message\r\n                zmq_msg_init (&amp;message);\r\n                zmq_msg_recv (&amp;message, backend, 0);\r\n                int more = zmq_msg_more (&amp;message);\r\n                zmq_msg_send (&amp;message, frontend, more? ZMQ_SNDMORE: 0);\r\n                zmq_msg_close (&amp;message);\r\n                if (!more)\r\n                    break; \/\/ Last message part\r\n            }\r\n        }\r\n    }\r\n\/\/ We never get here, but clean up anyhow\r\n    zmq_close (frontend);\r\n    zmq_close (backend);\r\n    zmq_ctx_destroy (context);\r\n    return 0;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>#include #include #include #include #include int main (void) { \/\/ Prepare our context and sockets void *context = zmq_ctx_new (); void *frontend = zmq_socket (context, ZMQ_ROUTER); void *backend = zmq_socket (context, ZMQ_DEALER); zmq_bind (frontend, &#8220;tcp:\/\/*:5559&#8221;); zmq_bind (backend, &#8220;tcp:\/\/*:5560&#8221;); \/\/ Initialize poll set zmq_pollitem_t items [] = { { frontend, 0, ZMQ_POLLIN, 0 }, { backend, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-205","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/205","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=205"}],"version-history":[{"count":2,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/205\/revisions"}],"predecessor-version":[{"id":348,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/205\/revisions\/348"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}