Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zeromq request-reply pattern between python and scala

I'm trying to implement a request-reply example using ZeroMQ Request-Reply Pattern between python and scala. Based on the code provided by the zmq documentation I managed to run successfully a requester in scala and the server in python.

scala TO python rrclient scala and rrserver python

Now I'm trying to do the opposite. A requester in python and a replier in scala.

python TO scala rrclient python and rrserver scala

Here is the python client code

import zmq

#  Prepare our context and sockets
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:5559")

#  Do 10 requests, waiting each time for a response
for request in range(1,11):
    socket.send(b"Hello")
    message = socket.recv()
    print("Received reply %s [%s]" % (request, message))

And here is the scala code

import org.zeromq.ZMQ
import org.zeromq.ZMQ.{Context,Socket}

object rrserver {
def main(args : Array[String]) {
    //  Prepare our context and socket
    val context = ZMQ.context(1)
    val receiver = context.socket(ZMQ.REP)
    receiver.connect("tcp://localhost:5559")

    while (true) {
        //  Wait for next request from client
        //  We will wait for a 0-terminated string (C string) from the client,
        //  so that this server also works with The Guide's C and C++ "Hello World" clients

        //IT BLOCKS HERE
        val request = receiver.recv (0)
        println(request)
        // In order to display the 0-terminated string as a String,
        // we omit the last byte from request
        // println ("Received request: [" + new String(request,0,request.length-1)  
        //  Creates a String from request, minus the last byte
        //                        + "]")

        //  Do some 'work'
        // try {
        //   Thread.sleep (1000)
        // } catch  {
        //   case e: InterruptedException => e.printStackTrace()
        // }

        // Send reply back to client
        // We will send a 0-terminated string (C string) back to the client,
        // so that this server also works with The Guide's C and C++ "Hello World" clients
        // val reply = "World ".getBytes
        // reply(reply.length-1)=0 //Sets the last byte of the reply to 0
        // receiver.send(reply, 0)
    }
}

}

On the scala example it says it expects a 0-terminated string as a c-string.

I have already tried to send a simple string from python and a 0-terminated string, but none of them have worked.

sending in python:

socket.send(b"Hello")
//or
socket.send(b"hello\x00")

receiving in scala:

//result is always null here
val request = receiver.recv (0)

What I'm I doing wrong? I feel like is something related to the python string, but I couldn't solve it yet.

like image 660
Arian Pasquali Avatar asked Jun 27 '26 22:06

Arian Pasquali


1 Answers

I have encountered the same problem. And finally I found that I used the wrong version of ZeroMQ.

Here are the versions of the resources I use:

  1. scala-binding: zeromq-scala-binding_2.11.0-M3-0.0.7

  2. scala: Scala-2.11.7

  3. zeromq: Stable Release 2.2.0

  4. jnr: jnr-constants-0.8.2

  5. jna: jna-3.0.9

As stated in README.md on the homepage of scala binding of zeromq (https://github.com/valotrading/zeromq-scala-binding) that

The Scala binding for ZeroMQ is based on ZeroMQ versions 2.1.x ...

At the very beginning, I used the latest version of zeromq 4.0.4 and I tried to use py-zmq to send message and use scala-zmq to recv message. But I always found socket in scala receives nothing (returning null) ...

And you will then understand why http://doc.akka.io/docs/akka/2.3.9/java/zeromq.html puts a note that

The currently used version of zeromq-scala-bindings is only compatible with zeromq 2; zeromq 3 is not supported.

By the way, I implement this using eclipse on windows.

!! At last, welcome to use my version: https://github.com/zzxx-husky/zeromq-scala-binding

like image 101
Zhao Yunjian Avatar answered Jun 30 '26 11:06

Zhao Yunjian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!