Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <zmq.h>
- #include "multiPartMsg.h"
- void* context = zmq_ctx_new();//create context
- void* CreateDealerSocket(const char* svrAddr)
- {
- void* dealerSocket = zmq_socket(context, ZMQ_DEALER);//create socket
- int linger(100);
- size_t linger_size = sizeof (&linger);
- int rc = zmq_setsockopt (dealerSocket, ZMQ_LINGER, &linger, linger_size);
- if (rc != 0)
- {
- std::string errStr = zmq_strerror(zmq_errno());
- std::string errConc = "dealerClient: Error Setting Linger Value: " + errStr;
- const char* errOut = errConc.c_str();
- OutputDebugStringA(errOut);
- }
- rc = zmq_connect(dealerSocket, svrAddr);//connect socket
- if (rc != 0)
- {
- std::string errStr = zmq_strerror(zmq_errno());
- std::string errConc = "dealerClient: Error Connecting Socket: " + errStr;
- const char* errOut = errConc.c_str();
- OutputDebugStringA(errOut);
- }
- return dealerSocket;//success
- }
- int main()
- {
- std::cout << "dealerClient: Preparing" << std::endl;
- const char* serverAddress = "tcp://localhost:5380";
- void* dlrSck = CreateDealerSocket(serverAddress);
- std::cout << "dealerClient: Dealer Socket Ready" << std::endl;
- //Prepare & send multipart message
- std::vector <std::string> outVec;
- outVec.push_back("Frame1");
- outVec.push_back("Frame2");
- outVec.push_back("Frame3");
- outVec.push_back("Frame4");
- outVec.push_back("Frame5");
- MultiPartMessage mpm(dlrSck, "NULL", outVec);//"NULL" indicates identity frame is not required on this message
- std::cout << "dealerClient: MultiPartMessage Sent" << std::endl;
- //Await reply
- std::cout << "dealerClient: Awaiting Reply" << std::endl;
- MultiPartMessage rmpm(dlrSck);//blocking receive
- size_t parts = rmpm.getNumberOfFrames();
- std::cout << "--------------------------- Message Received ---------------------------" << std::endl;
- for(size_t i = 0; i != parts; i++)
- {
- std::cout << "Vector Index: " << i << " Frame: " << rmpm.getFrameElement(i) << std::endl;
- }
- std::cout << "------------------------------------------------------------------------" << std::endl;
- //Tidy up
- int rc = zmq_disconnect(dlrSck, serverAddress);
- if (rc != 0)
- {
- std::string errStr = zmq_strerror(zmq_errno());
- std::string errConc = "dealerClient: Error Disconnecting Socket: " + errStr;
- const char* errOut = errConc.c_str();
- OutputDebugStringA(errOut);
- }
- std::cout << "dealerClient: Disconnected From Router" << std::endl;
- rc = zmq_close(dlrSck);
- if (rc != 0)
- {
- std::string errStr = zmq_strerror(zmq_errno());
- std::string errConc = "dealerClient: Error Closing Socket: " + errStr;
- const char* errOut = errConc.c_str();
- OutputDebugStringA(errOut);
- }
- std::cout << "dealerClient: Socket Closed" << std::endl;
- rc = zmq_ctx_term(context);//if sockets are not closed with zmq_close() then zmq_ctx_term() will hang forever.
- if (rc != 0)
- {
- std::string errStr = zmq_strerror(zmq_errno());
- std::string errConc = "dealerClient: Error Terminating Context " + errStr;
- const char* errOut = errConc.c_str();
- OutputDebugStringA(errOut);
- }
- std::cout << "dealerClient: Context Terminated and Exiting Program" << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment