Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // zmq_test.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include "zmq.hpp"
- #pragma comment(lib, "libzmq.lib")
- zmq::context_t * ctx;
- zmq::socket_t * appSocket;
- HANDLE hTh;
- DWORD idTh;
- DWORD WINAPI ThreadProc( LPVOID lpParam )
- {
- zmq::socket_t * thSocket = (zmq::socket_t *)lpParam;
- zmq::message_t net_msg;
- try
- {
- if (thSocket->recv(&net_msg, 0))
- {
- printf("received: %d bytes\n", net_msg.size());
- }
- }
- catch (zmq::error_t e)
- {
- printf("recieve error: %s", e.what());
- }
- printf("thread out");
- return 0;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- ctx = new zmq::context_t(1);
- try
- {
- appSocket = new zmq::socket_t(*ctx, ZMQ_SUB);
- appSocket->connect("tcp://192.168.36.91:18001");
- appSocket->setsockopt(ZMQ_SUBSCRIBE, "", 0);
- }
- catch(zmq::error_t e)
- {
- printf("socket init error: %s", e.what());
- delete ctx;
- return 1;
- }
- hTh = CreateThread(NULL, 0, ThreadProc, appSocket, CREATE_SUSPENDED, &idTh);
- if (INVALID_HANDLE_VALUE == hTh)
- {
- delete ctx;
- return 1;
- }
- ResumeThread(hTh);
- Sleep(1000);
- // zmq_close not return when recieving is active on ZMQ_SUB socket
- delete appSocket;
- Sleep(1000);
- delete ctx;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement