Android NDK套接字创建空指针
Android NDK socket creation null pointer
为了添加 TCP 套接字选项,我需要使用 Android NDK 的 C 套接字而不是 Java 套接字。以下函数由按钮单击事件调用。
创建这样的套接字时,系统调用将我引向空指针(请参阅最后的日志输出)。
如日志所示,上面的行被调用(创建套接字)但是随后(因此它必须在 socket() 调用中),应用程序在可以评估之前由于空指针而崩溃return 值。
我做错了什么?
#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
从 void error() 函数中删除 "exit(0)"
为了添加 TCP 套接字选项,我需要使用 Android NDK 的 C 套接字而不是 Java 套接字。以下函数由按钮单击事件调用。
创建这样的套接字时,系统调用将我引向空指针(请参阅最后的日志输出)。
如日志所示,上面的行被调用(创建套接字)但是随后(因此它必须在 socket() 调用中),应用程序在可以评估之前由于空指针而崩溃return 值。
我做错了什么?
#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
从 void error() 函数中删除 "exit(0)"