[zeromq-dev] First try - Java client, CPP server

Deepak Jharodia deepakjharodia at gmail.com
Mon Feb 11 07:40:06 CET 2013


Hi,

I'm starting with ZMQ and created my first program. The client is in Java
and server in CPP, using respective bindings. I have three questions:
1) I want client to connect only if server is running(on that IP). Right
now, client connects even if I give a random IP.
2) I want to start server and *then* start client and send requests.
However, if I do this nothing happens. But if I start client first and send
a request and then start server, it receives a packet.
3) The way I’m trying to extract message, I get first character of message
correct. Rest is all garbage. Am I doing something wrong?

Client code:
ZMQ.Context context = ZMQ.context(1);
socket = context.socket(ZMQ.REQ);
try { socket.connect("tcp://10.199.88.134:6669");
     } catch (ZMQException e) {
System.out.println(e.getMessage());
     }
String outData = new String();
outData = "list";
socket.send(outData.getBytes(), outData.length());



Server code:
zmq::context_t context (1);
zmq::socket_t socket (context, ZMQ_REQ);
try {
   socket.bind ("tcp://10.199.88.133:6669");
} catch(zmq::error_t e) {
   return 0;
}
zmq::message_t request;
while(1) {
   std::cout << "Waiting for packet.." << std::endl;

   int size = -2;
   try {
      size = socket.recv (&request);
   } catch(zmq::error_t e) {
      return 0;
   }
   if (size == -1) {
      return 0;
   } else {
      cout << "Message length: "<<size << endl;}
   char *message = (char*)malloc (size + 1);
   memcpy (message, request.data(), size);
   std::string message_string(message);
   cout << message_string << endl;

   string outData = "OK";
   int length = outData.length();
   zmq::message_t reply (length);
   memcpy ((void *) reply.data (), outData.c_str(),
strlen(outData.c_str())+1);
   socket.send (request);
}


Cheers,
Deepak
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20130211/1873053e/attachment.htm>


More information about the zeromq-dev mailing list