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

Ivan Pechorin ivan.pechorin at gmail.com
Wed Feb 13 21:00:58 CET 2013


Besides the zeromq usage issue(s), you have charset encoding mismatch.

outData.getBytes() returns your string in UTF-16 encoding. When you receive
this byte buffer in your CPP server, you just pass it to constructor of
std::string that accepts it until the first 0 byte. In case of latin text
encoded in UTF-16, either the first byte, or the second byte will be 0
(depending on endianness).

You should
1) use outData.getBytes("UTF-8"), and
2) take length fom the byte buffer, not String.
11.02.2013 10:40 пользователь "Deepak Jharodia" <deepakjharodia at gmail.com>
написал:

> 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
>
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20130214/2466ae48/attachment.html>


More information about the zeromq-dev mailing list