为了添加TCP-socket选项,我需要使用Android NDK的C-socket而不是Java套接字。按钮单击事件调用以下函数。
创建这样的套接字时,系统调用会将我引导到空指针(请参阅末尾的日志输出)。
如日志所示,上面的行被调用(Create Socket)然后(因此它必须在socket() - 调用中),应用程序因为空指针而崩溃,然后才能评估返回值值。
我做错了什么?
#include <jni.h>
#include <string.h>
#include <android/log.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define DEBUG_TAG "NDK_AndroidNDK1SampleActivity"
void error(const char *msg)
{
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:LC: [%s]", msg);
exit(0);
}
int Java_com_example_ndk1_AndroidNDK1SampleActivity_sendUrgent(JNIEnv * env, jobject this,
jstring jurl, int portno, jstring jdata, jboolean jSetUrgentFlag)
{
int sockfd = 0;
error("Create Socket");
sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
error("Socket created");
if (sockfd < 0)
error("ERROR opening socket");
return 1;
}
05-18 14:47:54.002 11500-11500/com.example.ndk1 D/NDK_AndroidNDK1SampleActivity﹕ NDK:LC: [Create Socket]
05-18 14:47:54.022 780-1221/? I/WindowState﹕ WIN DEATH: Window{42bde4f8 u0 com.example.ndk1/com.example.ndk1.AndroidNDK1SampleActivity}
05-18 14:47:54.022 780-1222/? I/ActivityManager﹕ Process com.example.ndk1 (pid 11500) has died.
05-18 14:47:54.022 780-1222/? W/ActivityManager﹕ Force removing ActivityRecord{42ae6278 u0 com.example.ndk1/.AndroidNDK1SampleActivity t17}: app died, no saved state
05-18 14:47:54.062 988-1000/? W/Binder﹕ Caught a RuntimeException from the binder stub implementation.
java.lang.NullPointerException
at android.inputmethodservice.IInputMethodWrapper.setSessionEnabled(IInputMethodWrapper.java:280)
at com.android.internal.view.IInputMethod$Stub.onTransact(IInputMethod.java:129)
at android.os.Binder.execTransact(Binder.java:404)
at dalvik.system.NativeStart.run(Native Method)
dmesg-log:
<3>[11101.768759] init: untracked pid 11500 exited
答案 0 :(得分:0)
删除&#34;退出(0)&#34;来自void error()函数