Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. // zmq_test.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include "zmq.hpp"
  5. #pragma comment(lib, "libzmq.lib")
  6.  
  7. zmq::context_t * ctx;
  8. zmq::socket_t * appSocket;
  9.  
  10. HANDLE hTh;
  11. DWORD idTh;
  12.  
  13. DWORD WINAPI ThreadProc( LPVOID lpParam )
  14. {
  15.   zmq::socket_t * thSocket = (zmq::socket_t *)lpParam;
  16.   zmq::message_t net_msg;
  17.  
  18.   try
  19.   {
  20.     if (thSocket->recv(&net_msg, 0))
  21.     {
  22.       printf("received: %d bytes\n", net_msg.size());
  23.     }
  24.   }
  25.   catch (zmq::error_t e)
  26.   {
  27.     printf("recieve error: %s", e.what());
  28.   }
  29.  
  30.   printf("thread out");
  31.   return 0;
  32. }
  33.  
  34. int _tmain(int argc, _TCHAR* argv[])
  35. {
  36.   ctx = new zmq::context_t(1);
  37.  
  38.   try
  39.   {
  40.     appSocket = new zmq::socket_t(*ctx, ZMQ_SUB);
  41.     appSocket->connect("tcp://192.168.36.91:18001");
  42.     appSocket->setsockopt(ZMQ_SUBSCRIBE, "", 0);
  43.   }
  44.   catch(zmq::error_t e)
  45.   {
  46.     printf("socket init error: %s", e.what());
  47.     delete ctx;
  48.     return 1;
  49.   }
  50.  
  51.   hTh = CreateThread(NULL, 0, ThreadProc, appSocket, CREATE_SUSPENDED, &idTh);
  52.  
  53.   if (INVALID_HANDLE_VALUE == hTh)
  54.   {
  55.     delete ctx;
  56.     return 1;
  57.   }
  58.  
  59.   ResumeThread(hTh);
  60.  
  61.   Sleep(1000);
  62.   // zmq_close not return when recieving is active on ZMQ_SUB socket
  63.   delete appSocket;
  64.  
  65.   Sleep(1000);
  66.   delete ctx;
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement