問題描述
如果服務器無法訪問,則在 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.