调用 socket.bind() 时应用程序关闭 - Android 上的 JeroMQ

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

我是一般的消息队列编程新手,我一直在尝试开发一个使用 ZeroMQ 的简单 Android 应用程序。我 运行 服务器和客户端程序都在同一设备上的不同线程中。这是我的 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();
}

这是客户端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));
}

这是主要 activity 的片段,它在另一个线程中启动服务器并实现一个 clickListener 来调用客户端。

@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();
                }
            });
}

我遇到的问题是应用程序一启动就自动关闭。当我从服务器中删除 socket.bind("tcp://*:5570"); 行时,它不再关闭,但也无法按预期方式工作,因为服务器不会收听。

我在清单中同时拥有 INTERNETACCESS_WIFI_STATE 权限。

在此问题上提供任何帮助表示感谢。

编辑: 我在 .bind() 行放置了一个断点,这是我得到的错误:

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: 完整 logcat 错误:

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.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.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.call(AsyncTask.java:295)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor.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.call(AsyncTask.java:295) 
            at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
            at android.os.AsyncTask$SerialExecutor.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

看起来像一个简单的 classNotFound 错误,但它指的是 "android package" 附带的 class,这意味着它不属于任何外部库。那怎么会找不到呢?

也许删除 * 并将其替换为 localhost

规则 0) 习惯于始终调用 Poller.poll( aTimeoutInMILLISECONDs ) 方法,以便保持控制在外部事件循环生态系统中需要服务哪些事件(如果有的话)

规则 1) 永远不要在严肃的生产级代码中使用 REQ/REPbecause of it's inherent hard-wired affinity to fall into an un-salvageable mutual dead-lock.

规则 2) 总是喜欢显式地址 .bind()-s。隐式和通配符扩展不是策划生产级部署的一种方式,因为您的系统将依赖于外部翻译——而不是 .bind() 到一个显式的全局广告地址(即使教科书示例显示其他做法......)

规则 3) 毫不犹豫地使用 assert();try: / except: 处理实践,这样您就可以充分定位和诊断根本原因任何例外。大多数语言绑定都提供 ZeroMQ 错误工具。

您 运行 遇到的问题是您使用的 JeroMQ 版本与 Android 不兼容。此拉取请求:https://github.com/zeromq/jeromq/pull/471 添加了对 Android 10 及更高版本的支持,但仅适用于 0.4.3-SNAPSHOT。