Search code examples
javaandroidsocketszeromqjeromq

Application closes when socket.bind() is called - JeroMQ on Android


I'm new to programming with Message Queues in general and I've been trying to work on a simple Android Application that uses ZeroMQ. I'm running both server and client program on the same device in separate Threads. This is my Server class:

public class ZeroMQServer implements Runnable{
private Handler uiThreadHandler;

public ZeroMQServer(Handler uiThreadHandler) {
    this.uiThreadHandler = uiThreadHandler;
}

@Override
public void run() {
    ZMQ.Context context = ZMQ.context(1);
    ZMQ.Socket socket = context.socket(ZMQ.REP);
    socket.bind("tcp://*:5570");

    while(!Thread.currentThread().isInterrupted()) {
        byte[] msg = socket.recv(0);
        uiThreadHandler.sendMessage(
                Util.bundledMessage(uiThreadHandler, new String(msg)));
        socket.send(Util.reverseInPlace(msg)+"", 0);
    }
    socket.close();
    context.term();
}

And this is the client class:

public class ZeroMQMessageTask extends AsyncTask<String, Void, String> {
private final Handler uiThreadHandler;

public ZeroMQMessageTask(Handler uiThreadHandler){
    this.uiThreadHandler = uiThreadHandler;
}

@Override
protected String doInBackground(String... params) {
    ZMQ.Context context = ZMQ.context(1);
    ZMQ.Socket socket = context.socket(ZMQ.REQ);
    socket.connect("tcp://localhost:5570");

    socket.send(params[0].getBytes(), 0);
    String result = new String(socket.recv(0));

    socket.close();
    context.term();

    return result;
}

@Override
protected void onPostExecute(String result) {
    uiThreadHandler.sendMessage(Util.bundledMessage(uiThreadHandler, result));
}

And this is the snippet of the main activity that starts the server in another Thread and implements a clickListener to call the client.

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

    textView = (TextView)findViewById(R.id.text_console);
    editText = (EditText)findViewById(R.id.text_message);

    new Thread(new ZeroMQServer(serverMessageHandler)).run();

    findViewById(R.id.button_send_message).setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new ZeroMQMessageTask(clientMessageHandler).execute(getTaskInput());
                }

                protected String getTaskInput() {
                    return editText.getText().toString();
                }
            });
}

The problem I'm having is that as soon as the application starts it closes automatically. When I remove the socket.bind("tcp://*:5570"); line from the server, it doesn't close anymore, but also doesn't work the way it's supposed to, as the server won't listen.

I have both INTERNET and ACCESS_WIFI_STATE permissions granted in the manifest.

Any help in this matter is appreciated.

EDIT: I've put a breakpoint at the .bind() line, and this is the error I get:

Didn't find class "java.net.StandardProtocolFamily" on path:
  DexPathList[
    [zip file "/data/app/com.example.victor.zeromqrabbitmq-1/base.apk",
     zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_dependencies_apk.apk",
     zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_0_apk.apk",
     zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_1_apk.apk",
     zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_2_apk.apk",
     zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_3_apk.apk",
     zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_4_apk.apk",
     zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_5_apk.apk",
     zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_6_apk.apk",
     zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_7_apk.apk",
     zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_8_apk.apk",
     zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_9_apk.apk"
     ],
  nativeLibraryDirectories=[
     /data/app/com.example.victor.zeromqrabbitmq-1/lib/arm,
     /vendor/lib, /system/lib
     ]
  ]

EDIT2: Full logcat error:

09-01 00:44:19.878 13082-13205/com.example.victor.zeromqrabbitmq E/Error: Exception: 
09-01 00:44:20.188 13082-13205/com.example.victor.zeromqrabbitmq E/AndroidRuntime: FATAL EXCEPTION:
      AsyncTask #1
        Process: com.example.victor.zeromqrabbitmq, PID: 13082
        java.lang.RuntimeException: An error occurred while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:309)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
            at java.lang.Thread.run(Thread.java:818)
         Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Ljava/net/StandardProtocolFamily;
            at zmq.io.net.tcp.TcpAddress.family(TcpAddress.java:50)
            at zmq.io.net.tcp.TcpListener.setAddress(TcpListener.java:171)
            at zmq.SocketBase.bind(SocketBase.java:363)
            at org.zeromq.ZMQ$Socket.bind(ZMQ.java:2333)
            at com.example.victor.zeromqrabbitmq.ZeroMQServer.doInBackground(ZeroMQServer.java:24)
            at com.example.victor.zeromqrabbitmq.ZeroMQServer.doInBackground(ZeroMQServer.java:10)
            at android.os.AsyncTask$2.call(AsyncTask.java:295)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
            at java.lang.Thread.run(Thread.java:818) 
         Caused by: java.lang.ClassNotFoundException: Didn't find class "java.net.StandardProtocolFamily" on path: DexPathList[[zip file "/data/app/com.example.victor.zeromqrabbitmq-1/base.apk", zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.victor.zeromqrabbitmq-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.victor.zeromqrabbitmq-1/lib/arm, /vendor/lib, /system/lib]]
            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
            at zmq.io.net.tcp.TcpAddress.family(TcpAddress.java:50) 
            at zmq.io.net.tcp.TcpListener.setAddress(TcpListener.java:171) 
            at zmq.SocketBase.bind(SocketBase.java:363) 
            at org.zeromq.ZMQ$Socket.bind(ZMQ.java:2333) 
            at com.example.victor.zeromqrabbitmq.ZeroMQServer.doInBackground(ZeroMQServer.java:24) 
            at com.example.victor.zeromqrabbitmq.ZeroMQServer.doInBackground(ZeroMQServer.java:10) 
            at android.os.AsyncTask$2.call(AsyncTask.java:295) 
            at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
            at java.lang.Thread.run(Thread.java:818) 
            Suppressed: java.lang.ClassNotFoundException: java.net.StandardProtocolFamily
            at java.lang.Class.classForName(Native Method)
            at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
            at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
                    ... 13 more
         Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
09-01 00:44:20.699 13082-13206/com.example.victor.zeromqrabbitmq E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb810d418

Looks like a simple classNotFound error, but it refers to a class that comes with "android package", meaning it doesn't belong to any external libs. So how could it not be found?


Solution

  • The problem you are running into is the version of JeroMQ you are using isn't compatible with Android. This pull request: https://github.com/zeromq/jeromq/pull/471 added support for Android 10 and above but it only available on 0.4.3-SNAPSHOT.