Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // zmq_test_cli.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <conio.h>
- #include "zmq.hpp"
- #pragma comment(lib, "libzmq.lib")
- using namespace std;
- const char * connect_str = "tcp://192.168.36.91:5000";
- HANDLE hCliTh;
- DWORD idCliTh;
- DWORD msg_id = 0;
- DWORD WINAPI CliThreadProc( LPVOID lpParam )
- {
- zmq::context_t * ctx;
- zmq::socket_t * appSocket;
- ctx = new zmq::context_t(1);
- appSocket = new zmq::socket_t(*ctx, ZMQ_REQ);
- try
- {
- appSocket->connect(connect_str);
- cout << "client connect to: " << connect_str << endl;
- while (true)
- {
- Sleep(500);
- zmq::message_t send_msg(300);
- *((DWORD *)send_msg.data()) = msg_id;
- appSocket->send(send_msg);
- cout << ">>>>> " << msg_id << " >>>>>" << endl;
- msg_id++;
- zmq::message_t rcv_msg;
- bool b_rcv = false;
- for (int i = 10; i > 0; i--)
- {
- if ( appSocket->recv(&rcv_msg, ZMQ_NOBLOCK) )
- {
- DWORD rcv_msg_id = *((DWORD *)rcv_msg.data());
- b_rcv = true;
- cout << "<<<<< " << rcv_msg_id << " <<<<<" << endl;
- break;
- }
- else
- {
- Sleep(10);
- }
- }
- if (!b_rcv)
- {
- delete appSocket;
- appSocket = new zmq::socket_t(*ctx, ZMQ_REQ);
- appSocket->connect(connect_str);
- cout << "client reconnect to: " << connect_str << endl;
- }
- }
- }
- catch (zmq::error_t e)
- {
- cout << "zmq error" << e.what() << endl;
- }
- catch(...)
- {
- cout << "unexpected error" << endl;
- }
- delete appSocket;
- delete ctx;
- return 0;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- cout << "client started" << endl;
- hCliTh = CreateThread(NULL, 0, CliThreadProc, NULL, CREATE_SUSPENDED, &idCliTh);
- if (INVALID_HANDLE_VALUE == hCliTh)
- {
- return 1;
- }
- ResumeThread(hCliTh);
- while(true);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment