简体   繁体   中英

send a string from c++ to java through socket

I have a block of code on the c++ end that would pass a string like this:

char someName[100]="Some String here";
    send(sock,someName,sizeof(someName),0);

and on the other end I have a java code looking for a string message like this:

DataInputStream dIn= new DataInputStream(SOCK.getInputStream());
String filename=dIn.readUTF(); //Looks for "Some String here"

the code does not continue and gives a UTFDataFormatException. So I'm basically looking for conversion on c++ someName to UTF-8 format so both ends will be happy!

Thanks!

EDIT: I tried using BufferedReader on the java code and got something like:

‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~‡áø~

DataInputBuffer.ReadUTF() reads in a string encoded in modified utf-8 , which makes it a bad candidate for reading data that is not encoded with DataOutputBuffer.WriteUTF().

Also, your C++ program sends a fixed length string padded with zero's. This can also cause problem when the receiver doesn't expect this.

C++11 also supports UTF-8 string literals, see here .

从C / C ++应用程序将数据写入文本文件,然后从Java应用程序读取该文本文件。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM