0

I am trying to send a message from a Android Java client using JeroMQ to a Python server using ZeroMQ. I am running bother server and client on the same machine. The server runs in a Jupyter notebook, while the client runs in an Android device emulator on Android studio. The code is as follows. Python server:

context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5555")

while True:
    #  Wait for next request from client
    message = socket.recv_json()
    print("Received request: %s" % message)

    #  Do some 'work'
    time.sleep(1)

    #  Send reply back to client
    socket.send(b"World")

This is the Python client (which works):

context = zmq.Context()

#  Socket to talk to server 
print("Connecting to hello world server…") 
socket = context.socket(zmq.REQ) 
socket.connect("tcp://localhost:5555")

#  Do 10 requests, waiting each time for a response for request in range(3):
    print("Sending request %s …" % request)
    socket.send(b"Hellos")

    #  Get the reply.
    message = socket.recv()
    print("Received reply %s [ %s ]" % (request, message))

And this is my attempt at doing the same thing in a Java client:

public class MainActivity extends AppCompatActivity {

private final String TAG = "MainActivity";
private ZMQ.Socket req;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try  {
                try (ZContext context = new ZContext()) {
                    //  Socket to talk to server
                    req = context.createSocket(SocketType.REQ);
                    req.connect("tcp://localhost:5555");

                    for (int i = 0; i == 3; i++) {
                        req.send("Hello, this is from Java");
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    thread.start();
}}

The server doesn't seem to receive anything however. Any idea where I am going wrong?

speaker
  • 1
  • 2

0 Answers0