发送的java字符串作为C

发送的java字符串作为C

本文介绍了发送的java字符串作为C / C ++字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我工作的一个Android应用程序作为TCP服务器/客户端我想从应用程序将数据发送到服务器/这是写在C / C ++(与升压libraray制造)客户端。我有一个要求串converstion以字节的本地C函数一个普通的Java功能:

I'm working on a Android app that acts as TCP server/client i want to send data from the app to a server/client that is written in c/c++(made with the boost libraray). I have a normal Java function that calls a native c function for string converstion to bytes:

作为遵循的函数定义(本机的功能是转换字符串:

The function is defined as followed (the native function is Convert String:

// Send buffer, the method can be used by both client and server objects.
public void SendBuffer(String Buffer){

    try {
        // Convert char to string to byte
        byte[] Temp = new byte[10];

        String Teststring = "AAAAAAAABB";
        Temp = ConvertString(Teststring);

        //byte[] Temp = new String(Buffer).getBytes();

        // Get socket output stream
        OutputStream OutputBuffer = ClientSocket.getOutputStream();

        //Write byte data to outputstream
        OutputBuffer.write(Temp);

        // Neatly flush and close the outputbuffer
        OutputBuffer.flush();
        OutputBuffer.close();
    }
    catch (IOException e) {
        Log.e("TCPIPCommunicator: ", "Client: Failed to send", e);
        e.printStackTrace();
    }
}

功能ConvertString是,Java字符串转换为C / C ++字符串,并将其作为Java的字节的本地函数,因为跟着它的定义是:

The function ConvertString is a native function that converts the Java string to a C/C++ string and returns it as Java bytes, it is defined as followed:

JNIEXPORT jbyteArray JNICALL Java_com_example_communicationmoduleTCPIP_communicationmoduleTCPIP_ConvertString(
        JNIEnv * env, jobject,
        jstring Buffer)
{
        // Array to fill with data
        jbyteArray Array;

        // Init  java byte array
        Array = env->NewByteArray(10);

        const char* NewBuffer = env->GetStringUTFChars(Buffer, 0);


        // Set byte array region with the size of the SendData CommStruct.
        // Now we can send the data back.
        env->SetByteArrayRegion(Array, 0, 10, (jbyte*)NewBuffer);

        env->ReleaseStringUTFChars(Buffer, NewBuffer);


        // Return java array
        return Array;
    }

}

当我运行程序我得到C面而不是作为一个整体阵列(所以没有'AAAAAAAADD)两个AAAA。我认为,问题是,在服务器发送一次2'AAAA',而不是整个数组。客户端崩溃并出现以下错误:

When i run the program i get two 'AAAA' on the c side but not as a whole array ( so no 'AAAAAAAADD). I think the problem is that the server sends 2 'AAAA' and not the whole array at once. The client crashes with the following error:

的boost :: exception_detail :: clone_impl>
  什么():读:文件结束

'boost::exception_detail::clone_impl >' what(): read: End of file

请问Java服务器发送的数据错了吗?任何人都可以给我一个建议?所有的反馈是值得欢迎的!

Does the java server sends the data wrong? can anyone give me a suggestion? all feedback is welcome!

推荐答案

您愿意发送的数据或使JNI东西的工作?
在前一种情况下,用Java字符串转换成UTF-8转换(这将是英语ASCII)。

Are you willing to send the data or to make the JNI stuff work?In the former case, use Java to convert a string into UTF-8 (which will be ASCII for English.)

字节的文本转换[] - >字节[]是不是你需要的东西,但你会得到的想法:

Conversion of text byte[] -> byte[] is not exactly what you need, but you'll get the idea:

//byte[] result;
//byte[] source;
String s = new String(source,"UTF-8");
result = s.getBytes("UTF-16LE");

对于第二个情况,我可以分享工作code的一部分;它调用的Java从一种编码转换为另一种

For the 2nd case, I can share a portion of working code; it calls Java to convert from one encoding to another

// it returns NULL in the case of an exception
// the returned memory is calloc()'d; it's the caller's responsibility to free() it.
char* changeEncoding(const char*source, int len, int direction)
{
    JNIEnv* env = threadUnsafeInfo.env;
    jobject obj = threadUnsafeInfo.obj;

    if (!source) {
    JNU_ThrowByName(env, "java/lang/NullPointerException", 0);
    return NULL;
    }
    jbyteArray srcArray = env->NewByteArray(len);

    jclass cls = env->FindClass("com/xyz/MyClass");
    jmethodID mid = env->GetMethodID(cls, "convert", "([BI)[B");

    if (mid != NULL && srcArray != NULL) {
    env->SetByteArrayRegion(srcArray, 0, len, (jbyte*)source);
    env->ExceptionClear();

    //jbyteArray resArray = (jbyteArray)env->CallStaticObjectMethod(cls, mid, srcArray, direction);
    jbyteArray resArray = (jbyteArray)env->CallObjectMethod(obj, mid, srcArray, direction);
    if(env->ExceptionOccurred()) {
        DLOG("exception in convert ([BI)[B");
        env->ExceptionDescribe();
        //env->ExceptionClear(); // ??
        return NULL;
    }

    int resultLen = env->GetArrayLength(resArray);
    char* result = (char*)calloc(2 + resultLen,1); // why 2: a bit of healthy paranoia ain't gonna hurt anyone
    if (result == 0) {
        JNU_ThrowByName(env, "java/lang/OutOfMemoryError", 0);
        return NULL;
    }
    env->GetByteArrayRegion(resArray, 0, resultLen, (jbyte *)result);
    env->DeleteLocalRef(cls);
    env->DeleteLocalRef(resArray);
    env->DeleteLocalRef(srcArray);
    return result;
    } else {
    JNU_ThrowByName(env, "java/lang/NullPointerException", 0);
    myassert(("method id = 0",0));
    }
    return NULL;
}

在code,我手边
我没有用jstrings,preferring的字节数组。

In the code that I have at handI did not use jstrings, preferring the byte arrays.

这篇关于发送的java字符串作为C / C ++字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 15:54