0

我正在尝试向我的 android 应用程序编写一些本机代码,该应用程序使用 2 个文件管道在 java 代码和本机代码之间进行通信。最终目标是让本机代码通过网络进行通信,而 Java 代码只是为了监听管道两端的传入和传出数据。这是我从 java 应用程序调用的代码:

JNIEXPORT void JNICALL Java_com_test_fdtest_Bindings_openSession(JNIEnv *env, jobject this)
{
int inpipe[2];
int outpipe[2];
FILE *in;
FILE *out;
if(pipe (inpipe)) {
    __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "Unable to set up input pipe");
    return;
}
if(pipe (outpipe)) {
    __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "Unable to set up output pipe");
    return;
}
in = fdopen(inpipe[0], "w");
out = fdopen(outpipe[1], "r");
if(in == NULL) {
    __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "input pipe null");

}
if(out == NULL) {
    __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "output pipe null");

}
}

从这里我得到输出:

06-04 09:44:41.759: D/dalvikvm(1443): Trying to load lib /data/data/com.test.fdtest/lib/libfdtest.so 0x414955a8
06-04 09:44:41.789: D/dalvikvm(1443): Added shared lib /data/data/com.test.fdtest/lib/libfdtest.so 0x414955a8
06-04 09:44:41.789: D/NDK_BINDINGS(1443): input pipe null
06-04 09:44:41.799: D/NDK_BINDINGS(1443): output pipe null

这似乎很奇怪。似乎可以设置管道,但是打开文件失败。这段代码是否存在根本性的问题,因为文档似乎表明这会起作用。

谢谢你的帮助

4

1 回答 1

0

从手册页:

pipefd[0] refers to the read end of the pipe.  pipefd[1] refers to the
write end of the pipe.

您正在尝试打开读取端inpipe进行写入,并打开写入端outpipe进行读取。如果您打印errno,您可能会看到 EACCES。

于 2013-06-04T16:57:01.307 回答