kshitij singh
kshitij singh

Reputation: 363

Sending ZeroMQ data using TCP

I am trying to make a simple server.

A language I am restricted to use is c++.

I am using ZeroMQ.

I have creatred a simple server and a client, as in documentation.

ZeroMQ uses TCP instead of HTTP.

I know that HTTP's underlying layer is TCP, so I want to know will it have any performance issues by using TCP instead of HTTP.

And for HTTP I can use curl to test the application.

What should I use for TCP ( curl command to send request to a socket with a string parameter ).


Server:

#include <zmq.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>

int main (void)
{                                                  // Socket to talk to clients
   void *context = zmq_ctx_new ();
   void *responder = zmq_socket (context, ZMQ_REP);
   int rc = zmq_bind (responder, "tcp://*:5555");
   assert (rc == 0);

   while (1)
   {
      char buffer [10];
      zmq_recv (responder, buffer, 10, 0);
      printf ("Received Hello\n");

                                                   // trying to send json object
      zmq_send (responder, "World", 5, 0);
   // zmq_send (responder, "World", 5, 0);
      sleep (1);                                   // Do some 'work'
   }
   return 0;
}

client:

// Hello World client
// Connects REQ socket to tcp://localhost:5555
// Sends "Hello" to server, expects "World" back

#include <zmq.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
int main (void)
{
   void *context = zmq_ctx_new ();
                                                   // Socket to talk to server
   printf ("Connecting to hello world server...\n");
   void *requester = zmq_socket (context, ZMQ_REQ);
   zmq_connect (requester, "tcp://localhost:5555");
   int request_nbr;
   for (request_nbr = 0; request_nbr != 10; request_nbr++)
   {   
       zmq_msg_t request;
       zmq_msg_init_size (&request, 5);
       memcpy (zmq_msg_data (&request), "Hello", 5);
       printf ("Sending Hello %d...\n", request_nbr);
       zmq_msg_send (&request, requester, 0);
       zmq_msg_close (&request);
       zmq_msg_t reply;

       zmq_msg_init (&reply);
       zmq_msg_recv (&reply, requester, 0);
       printf ("Received World %d\n", request_nbr);
       zmq_msg_close (&reply);
   }
   zmq_close (requester);
   zmq_ctx_destroy (context);
   return 0;
}

Upvotes: 1

Views: 962

Answers (1)

user3666197
user3666197

Reputation: 1

Q1: will it have any performance issues by using TCP instead of HTTP?
A1: yes, it will. Both performance and latency will benefit from avoiding HTTP-rich-re-wrapping of data

Q2: What should I use for TCP to send a request to a socket with a string parameter?
A2: No command ( curl command ) will help you. ZeroMQ uses certain line-code ( assume it as a trivial protocol between communicationg peers ), so a standalone command-line tool will not be able to match the line-code requirement off-the-shelf. Solution? Create a simple c-programme, that will consume a cmd-line arguments ( the string, as an example ) and assemble a ZeroMQ-layer compatible data-framing so as to communicate with the remote peer. Also you shall notice, that for ZeroMQ REQ/REP Formal Communication Pattern to work, this proxy-tool will have to become the sole respective REQ, resp. REP entity in the step-forward-locking diadic-communication relation, thus also providing an awaited response, the REQ-side is expecting to receive after the REP-side has received a message.

Upvotes: 1

Related Questions