[zeromq-dev] new user question

derek baikie derek_baikie at hotmail.com
Sun Jul 26 17:22:20 CEST 2009



hi,

I have been trying to get a simple example running without success, and was wondering whether anyone would know where I am going wrong:

A brief overview of the program is that the server sends a string, and the client should log that the string is received.
When I start the server, I can see on telnet that data is being sent over the socket. However, when I start the client, it does not receive any data.

Moreover, if I ctrlC the client app, it makes the server app assert.

Any thoughts on what I'm doing wrong?

-Derek


/*
 * main.cpp
 *
 *  Created on: Jul 17, 2009
 *      Author: derek
 */

#include <iostream>
#include <zmq.hpp>

bool error_handler (const char*)
{
    //  Crash the exchange if a connection breaks.
    //  The results of the test would be irrelevent with network outages
    //  happening during the test anyway.
    return false;
}


class server
{
public:
    server(const char *host, const char *in_interface,const char *out_interface):
        dispatcher (2),
        locator (host)
    {
         //  Cause application to crash in case of network problems.
        zmq::set_error_handler (error_handler);

        //  Initialise 0MQ infrastructure.
        pt = zmq::io_thread_t::create (&dispatcher);
        api = zmq::api_thread_t::create (&dispatcher, &locator);



        //  Initialise the wiring.
        char tmp [256];
        zmq_snprintf (tmp, sizeof (tmp), "Q_%s", "TEST");
        api->create_queue (tmp, zmq::scope_global, in_interface, pt, 1, &pt);
        zmq_snprintf (tmp, sizeof (tmp), "E_%s", "TEST");
        eid = api->create_exchange (tmp, zmq::scope_global, out_interface, pt, 1, &pt);
        //api->bind ("OE", "OQ", pt, pt);

        // Wait for a while to create connections.
        sleep (1);
    }
    ~server(){}

    void run ()
    {
        //  Message dispatch loop.
        while (true) {
            std::cout << "send" << std::endl;
            sleep(1);
            std::string text ("THIS IS A TEST");
            zmq::message_t msg(text.size() + 1);
            memcpy(msg.data(), text.c_str(), text.size() + 1);
            api->send(eid, msg);
            std::cout << "sending" << std::endl;
        }
    }
private:
    zmq::dispatcher_t dispatcher;
    zmq::locator_t locator;
    zmq::api_thread_t *api;
    zmq::i_thread *pt;
    int eid;
};

class client
{
public:
    client(const char *host): //, const char *in_interface,const char *out_interface):
        dispatcher (2),
        locator (host)
    {
        //  Initialise 0MQ infrastructure.
        api = zmq::api_thread_t::create (&dispatcher, &locator);
        pt = zmq::io_thread_t::create (&dispatcher);

        //  Initialise the wiring.
        char tmp1 [256];
        zmq_snprintf (tmp1, sizeof (tmp1), "E_%s", "TEST");
        eid = api->create_exchange (tmp1);//, zmq::scope_global, out_interface, pt, 1, &pt);
        char tmp [256];
        zmq_snprintf (tmp, sizeof (tmp), "Q_%s", "TEST");
        api->bind (tmp1, tmp, pt, pt);

        // Wait for a while to create connections.
        sleep (1);

    }
    ~client(){}

    void run ()
    {
        //  Message dispatch loop.
        while (true) {
            std::cout << "About to receive" << std::endl;
            zmq::message_t msg;
            api->receive (&msg);
            //parse_message (&msg, this);
            std::cout << "Received MEssage" << std::endl;
        }
    }

private:
    zmq::dispatcher_t dispatcher;
    zmq::locator_t locator;
    zmq::api_thread_t *api;
    zmq::i_thread *pt;
    int eid;
};

int main(int argc, char *argv[])
{
    if ( (argc > 1) && (!(strcmp(argv[1], "Client"))) )
    {
        std::cout << "About to initialise" << std::endl;
        client c("192.168.0.3");
        std::cout << "About to run" << std::endl;
        c.run();
        std::cout << "running" << std::endl;
    }
    else {
        std::cout << "About to initialise" << std::endl;
        server s("192.168.0.3", "eth0:5555", "eth0:5556");
        std::cout << "About to run" << std::endl;
        s.run();
        std::cout << "running" << std::endl;
    }

    std::cout << "DROPPED THROUGH" << std::endl;

    return -1;
}


_________________________________________________________________
Bing™ brings you maps, menus, and reviews organized in one place. Try it now.
http://www.bing.com/search?q=restaurants&form=MLOGEN&publ=WLHMTAG&crea=TXT_MLOGEN_Local_Local_Restaurants_1x1
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20090726/39b9fc37/attachment.htm>


More information about the zeromq-dev mailing list