Ok so i found the solution. The "hashtx, hashblock" etc messages are sent as raw binary bytes which then need to be converted to hexadecimal.
Here is my code.
public static void main(String[] args) {
ZMQ.Context context = ZMQ.context(1);
// Connect our subscriber socket
ZMQ.Socket subscriber = context.socket(ZMQ.SUB);
// Synchronize with the publisher
subscriber.connect("tcp://192.168.137.211:28332");
//subscriber.subscribe("rawblock".getBytes());
//subscriber.subscribe("rawtx".getBytes());
//subscriber.subscribe("hashtx".getBytes());
subscriber.subscribe("hashblock".getBytes());
System.out.println("Subscribed.. Waiting for messages.");
while (true) {
ZMsg zMsg = ZMsg.recvMsg(subscriber);
System.out.println("New message received!");
int messageNumber = 0;
for (ZFrame f: zMsg) {
byte[] bytes = f.getData();
System.out.println("Message number: " + messageNumber + " | Byte array length: " + bytes.length);
if (messageNumber == 0){
String messageType = new String(bytes);
System.out.println("Message type: " + messageType);
} else if (messageNumber == 1) {
String message = Converter.bin2hex(bytes);
System.out.println("Message: " + message);
}
messageNumber++;
}
}
}
and the binary to hex converter class
import java.util.Formatter;
public class Converter
{
private Converter() {
throw new IllegalStateException("Utility class");
}
public static String bin2hex(byte[] bytes)
{
Formatter f = new Formatter();
try {
for (byte c : bytes)
f.format("%02X",c);
return (f.toString().toLowerCase());
} finally {
f.close();
}
}
public static String hex2bin(String hexString)
{
if (!hexString.matches("^[0-9a-fA-F]+$")) {
return null;
}
StringBuilder output = new StringBuilder();
for (int i = 0; i < hexString.length(); i+=2) {
String str = hexString.substring(i, i+2);
output.append((char)Integer.parseInt(str, 16));
}
return output.toString();
}
}