如果服務器無法訪問,則在 android 上運行的客戶端將關閉並退出應用程序,無異常或函數“connect (socket)”中的任何消息 (the client running on android closes and exit application without exception or any message in function “connect (socket)” if the server is unreachable)


問題描述

如果服務器無法訪問,則在 android 上運行的客戶端將關閉並退出應用程序,無異常或函數“connect (socket)”中的任何消息 (the client running on android closes and exit application without exception or any message in function “connect (socket)” if the server is unreachable)

我想將客戶端 TCP 從 android8/9/10 手機連接到 PC windows 10 服務器應用程序。如果服務器運行一切正常,但在 android 上運行的客戶端會關閉並退出應用程序,如果服務器無法訪問,則無異常或函數“connect (socket)”中的任何消息。這發生在 C++ ( android NDK) 和 Java 中。我正在使用 Android Studio 3.5.3。C++ 和 java 中的代碼部分是:

    …
    try {
       int ressc = connect(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
       if(ressc==0){
            …
       }
    }
    catch(...){
       std::exception_ptr p = std::current_exception();
    }
    ….

    /////Java

    …..
    try {
        InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
        socket = new Socket(serverAddr, SERVERPORT);
    } catch (SocketException e1) {
        e1.printStackTrace();
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    …

我試圖捕捉服務器無法訪問的情況。請幫我解決問題。

Logcat is:
020‑02‑06 16:02:11.909 18810‑18810/com.gstoilov.opencv_ndk V/OpenCV‑NDK‑Java: surfaceChanged format=4, width=1080, height=1080
2020‑02‑06 16:02:25.827 18810‑18810/com.gstoilov.opencv_ndk I/Choreographer: Skipped 1052 frames!  The application may be doing too much work on its main thread.
2020‑02‑06 16:02:25.907 18810‑18915/com.gstoilov.opencv_ndk I/OpenGLRenderer: Davey! duration=17620ms; Flags=1, IntendedVsync=817735142460428, Vsync=817752675793060, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=817752683403284, AnimationStart=817752683557816, PerformTraversalsStart=817752685522712, DrawStart=817752699312503, SyncQueued=817752735797243, SyncStart=817752735903232, IssueDrawCommandsStart=817752736699222, SwapBuffers=817752761843180, FrameCompleted=817752763080993, DequeueBufferDuration=5291000, QueueBufferDuration=202000, 
2020‑02‑06 16:02:25.926 18810‑18810/com.gstoilov.opencv_ndk D/AndroidRuntime: Shutting down VM
2020‑02‑06 16:02:25.931 18810‑18810/com.gstoilov.opencv_ndk E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.gstoilov.opencv_ndk, PID: 18810
    java.lang.IllegalArgumentException: width and height must be > 0
        at android.graphics.Bitmap.createBitmap(Bitmap.java:1042)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:1009)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:959)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:920)
        at com.gstoilov.opencv_ndk.MainActivity.JavaBitmapFromNativeMat(MainActivity.java:615)
        at com.gstoilov.opencv_ndk.MainActivity$3$1.run(MainActivity.java:366)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:280)
        at android.app.ActivityThread.main(ActivityThread.java:6706)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2020‑02‑06 16:02:25.942 18810‑18810/com.gstoilov.opencv_ndk I/Process: Sending signal. PID: 18810 SIG: 9

參考解法

方法 1:

Thank you for the idea and the lesson. After I checked the logcat I understood that the main thread tried to show a bitmap, but the socket was blocking for 30 seconds (default timeout) the current thread and couldn’t create this bitmap. The main thread was closing the VM and there were not emitted exception in the current thread.

(by gstoilovgstoilov)

參考文件

  1. the client running on android closes and exit application without exception or any message in function “connect (socket)” if the server is unreachable (CC BY‑SA 2.5/3.0/4.0)

#sockets #Android #connect






相關問題

Ці павінен UDP-сокет праходзіць працэс прыняцця, як TCP-сокеты? (Does UDP socket need to go through the Accept Process like TCP sockets?)

як запусціць каманду openssl з прыкладання java (how to run openssl command from a java application)

TypeError: непадтрымоўваны сокет тыпу аперанда Python (TypeError: unsupported operand type-python socket)

套接字編程:某些 ISP 是否對 FTP 上傳施加了速率限制? (Socket programming: Do some ISP's impose rate-limiting on FTP uploads?)

兩個進程使用同一個端口? (Two processes using the same port?)

Akka TCP 客戶端:如何使用 akka actor 通過 TCP 發送消息 (Akka TCP client: How can I send a message over TCP using akka actor)

無法運行 python-bluez RFCOMM 服務器示例腳本 (Cannot run python-bluez RFCOMM server example script)

Java中的套接字和進程 (Sockets and Processes in Java)

在網絡環境中從 Brother TD-4100N 打印機檢索打印機狀態 (Retrieving the printer status from the Brother TD-4100N printer in a network environment)

通過套接字連接發送 GUI/TUI (Sending a GUI/TUI Over a Socket Connection)

如果服務器無法訪問,則在 android 上運行的客戶端將關閉並退出應用程序,無異常或函數“connect (socket)”中的任何消息 (the client running on android closes and exit application without exception or any message in function “connect (socket)” if the server is unreachable)

使用 C 的 select() 系統調用為 UDP 應用程序創建多個客戶端和一個服務器 (Creating Multiple Client and One Server for UDP application using select() system call with C)







留言討論