[zeromq-dev] Creating a Simple HTTP Server using zmq_msg_send and zmq_send

Satish sa29.subramaniam at gmail.com
Mon Jul 7 05:03:11 CEST 2014

Hello Programmers

I am trying to get acquaint using zeromq library and am developing a 
program provided below on this forum.

I want to use zeromq message to retrieve HTTP message and send HTTP 
response to multiple client.

I have gone through zguide documentation but there is only a mention of 
Multiple client connecting to zeromq broker/process via InProcess (inproc).

I am deliberately not using inproc for queues purposes/ inter threading 
messaging as I want this done via TBB queuing.

However the crux of the issue I am not get the zeromq to send back a 
response to a web-browser using zmq_send and zmq_msg_send.

I have tried using zmq_msg_send because in runtime there is a possbility of 
have large data stream sent back to client/web browser.

I want to zmq_msg structure there is no need to specify the data 
limitations will receiving and sending stream from and to the client.

How can I solve problem?

Please help.

Thank you.



void release(void* data, void* hint){}

void main(int argc, char** argv){

    void* ZMQCONTEXT = ::zmq_ctx_new();

    std::istringstream &instr = std::istringstream();

    zmq_msg_t message = { "" };

    int init_status = 0, multi_status = 0;

    zmq_pollitem_t timer[2] = { { ::zmq_socket(ZMQCONTEXT, ZMQ_STREAM), 0, 
ZMQ_POLLIN, 0 }, { 0, 0, ZMQ_POLLOUT, 0 } };
    int POLLED_SIZE = sizeof(timer) / sizeof(zmq_pollitem_t);

    timer[1].socket = timer[0].socket;

    const char *tcp_desc = "tcp://*:9090";
    ::zmq_bind(timer[0].socket, tcp_desc);

    char http_response[100] =
        "HTTP/1.0 200 OK\r\n"
        "Content-Type: text/plain\r\n"
        "Hello, World!";

    int packet_size = sizeof(http_response) / sizeof(char);




        for (int p = 0; p < POLLED_SIZE; p++){

            ::zmq_poll(&timer[p], 1, 10);


        ::zmq_msg_recv(&message, timer[0].socket, 0);
        while (::zmq_msg_more(&message) == 1)
            ::zmq_msg_recv(&message, timer[0].socket, ZMQ_DONTWAIT);


        std::cout << std::endl << "Size = " << ::zmq_msg_size(&message) << 

        std::cout << "Data Received = " << instr.str() << std::endl;

        //send back the response

        //Prefix the socket with port id

        ::zmq_send(timer[0].socket, instr.str().c_str(), 
instr.str().size(), ZMQ_SNDMORE);

        //Send payload
        ::zmq_send(timer[0].socket, http_response, packet_size, 

        //Postfix the socket with port id
        ::zmq_send(timer[0].socket, instr.str().c_str(), 
instr.str().size(), ZMQ_SNDMORE);

        ::zmq_send(timer[0].socket, 0, 0, ZMQ_SNDMORE);

        std::cout << "Sending..." << http_response;

    } while (std::cin.get() == 0xd);

    for (int p = 0; p < POLLED_SIZE; p++){

        ::zmq_unbind(timer[p].socket, tcp_desc);

    ::zmq_ctx_shutdown(&ZMQCONTEXT);    ::zmq_ctx_term(&ZMQCONTEXT);


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20140706/3db3909a/attachment.htm>

More information about the zeromq-dev mailing list