Guest User

Oleg Sevostyanov

a guest
Nov 16th, 2010
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. // zmq_test_cli.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <conio.h>
  6. #include "zmq.hpp"
  7. #pragma comment(lib, "libzmq.lib")
  8.  
  9. using namespace std;
  10.  
  11. const char * connect_str = "tcp://192.168.36.91:5000";
  12.  
  13. HANDLE hCliTh;
  14. DWORD idCliTh;
  15.  
  16. DWORD msg_id = 0;
  17.  
  18. DWORD WINAPI CliThreadProc( LPVOID lpParam )
  19. {
  20.   zmq::context_t * ctx;
  21.   zmq::socket_t * appSocket;
  22.  
  23.   ctx = new zmq::context_t(1);
  24.   appSocket = new zmq::socket_t(*ctx, ZMQ_REQ);
  25.  
  26.   try
  27.   {
  28.     appSocket->connect(connect_str);
  29.     cout << "client connect to: " << connect_str << endl;
  30.  
  31.     while (true)
  32.     {
  33.       Sleep(500);
  34.       zmq::message_t send_msg(300);
  35.      
  36.       *((DWORD *)send_msg.data()) = msg_id;
  37.  
  38.       appSocket->send(send_msg);
  39.       cout << ">>>>> " << msg_id << " >>>>>" << endl;
  40.  
  41.       msg_id++;
  42.  
  43.       zmq::message_t rcv_msg;
  44.       bool b_rcv = false;
  45.  
  46.       for (int i = 10; i > 0; i--)
  47.       {
  48.         if ( appSocket->recv(&rcv_msg, ZMQ_NOBLOCK) )
  49.         {
  50.           DWORD rcv_msg_id = *((DWORD *)rcv_msg.data());        
  51.           b_rcv = true;
  52.  
  53.           cout << "<<<<< " << rcv_msg_id << " <<<<<" << endl;
  54.           break;
  55.         }
  56.         else
  57.         {
  58.           Sleep(10);
  59.         }
  60.       }
  61.  
  62.       if (!b_rcv)
  63.       {
  64.         delete appSocket;
  65.         appSocket = new zmq::socket_t(*ctx, ZMQ_REQ);
  66.         appSocket->connect(connect_str);
  67.         cout << "client reconnect to: " << connect_str << endl;
  68.       }
  69.  
  70.     }
  71.   }
  72.   catch (zmq::error_t e)
  73.   {
  74.     cout << "zmq error" << e.what() << endl;
  75.   }
  76.   catch(...)
  77.   {
  78.     cout << "unexpected error" << endl;
  79.   }
  80.  
  81.   delete appSocket;
  82.   delete ctx;
  83.   return 0;
  84. }
  85.  
  86. int _tmain(int argc, _TCHAR* argv[])
  87. {
  88.   cout << "client started" << endl;
  89.  
  90.   hCliTh = CreateThread(NULL, 0, CliThreadProc, NULL, CREATE_SUSPENDED, &idCliTh);
  91.  
  92.   if (INVALID_HANDLE_VALUE == hCliTh)
  93.   {
  94.     return 1;
  95.   }
  96.  
  97.   ResumeThread(hCliTh);
  98.  
  99.   while(true);
  100.  
  101.   return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment