-1

I am trying to connect to a bitcoin node using the ZMQ library for Java. the problem is that when I try to receive a response the code remains frozen. Returns nothing.

This is my code:

public class CBETest {

    private static final String TEST_URL = "obelisk.airbitz.co";

    public static void main(String[] args) {

       System.out.println("\t--- ZMQ ---");
       Ctx c = zmq.ZMQ.createContext();
       SocketBase s = c.createSocket(zmq.ZMQ.ZMQ_DEALER);

       zmq.ZMQ.connect(s, "tcp://"+TEST_URL+":9091");
       System.out.println("Connected!");
       int sent = zmq.ZMQ.send(s, "blockchain.fetch_last_height", 0);

       System.out.println("Sent: " + sent);
       Msg msg = zmq.ZMQ.recv(s, 0);
       System.out.println("Response " + Arrays.toString(msg.data()));
    }
}

The code freezes in the line Msg msg = zmq.ZMQ.recv(s, 0);. I am using the calls described here for the full node implemetation. Thanks in advance!

Ander Acosta
  • 1,060
  • 1
  • 12
  • 25

1 Answers1

0

The code is not freezing, it is blocking while waiting to receive a message.

I would suggest you put your above code in a thread/runnable class and use localhost as the TEST_URL and start the server.

Then create another Runnable class with a client that tries to connect to that port and send back a message and start that thread and see if the message gets through.

There is an example here: http://zguide.zeromq.org/java:rtdealer

HulkSmash
  • 386
  • 1
  • 6