简体   繁体   English

从C ++套接字读取Java中的数据

[英]Read data in Java from C++ socket

I wrote this program, but it never reaches line b . 我写了这个程序,但是没有达到line b I am sure the loop is completing. 我确定循环已经完成。 Does anyone know what is wrong? 有人知道哪里出问题了吗? :D thanks :D谢谢

while((x = inr.read(chars, 0, chars.length)) != -1){
    result += String.valueOf(chars[0]);
    Log.d("@@@", "a " + result);  // line a
};
Log.d("@@@", "a " + result); // line b

Output 产量

05-31 10:18:20.249: D/@@@(676): Starting iMNSConnection...
05-31 10:18:20.249: D/@@@(676): Is trying to connect to server...
05-31 10:18:20.289: D/@@@(676): a W
05-31 10:18:20.289: D/@@@(676): a We
05-31 10:18:20.289: D/@@@(676): a Wel
05-31 10:18:20.289: D/@@@(676): a Welc
05-31 10:18:20.289: D/@@@(676): a Welco
05-31 10:18:20.294: D/@@@(676): a Welcom
05-31 10:18:20.294: D/@@@(676): a Welcome
05-31 10:18:20.294: D/@@@(676): a Welcome 
05-31 10:18:20.294: D/@@@(676): a Welcome !
05-31 10:18:20.294: D/@@@(676): a Welcome !!
05-31 10:18:20.294: D/@@@(676): a Welcome !!!
05-31 10:18:20.299: D/dalvikvm(676): GC_CONCURRENT freed 1196K, 36% free 16727K/25991K, paused 1ms+2ms, total 15ms
05-31 10:18:20.299: D/dalvikvm(676): WAIT_FOR_CONCURRENT_GC blocked 13ms

Edited 编辑

Actuall I am using a C++ Server with Java client 实际上,我正在使用带有Java客户端的C ++服务器

C++ side C ++方面

char* classroomList = "{........}";
send(ConnectedSocket, classroomList, strlen(classrooomList), 0);

Then the client silde (Java) If I use BufferedReader and Print out nothing. 然后客户端silde(Java)如果我使用BufferedReader并什么也不打印。 So I use this, 所以我用这个

InputStreamReader inr = new InputStreamReader(ins);
char[] chars = new char[1024];
while((x = inr.read(chars, 0, chars.length - 1)) != -1){
    result += String.valueOf(chars);
}

The Output like this: 输出是这样的:

05-31 10:47:32.464: D/@@@(14850): Is trying to connect to server...
05-31 10:47:32.494: D/@@@(14850): 11a Welcome !!!������...(and 2028 same characters)

So I try to add this 所以我尝试添加

while((x = inr.read(chars, 0, chars.length - 1)) != -1){
    chars[x] = '\0';
    result += String.valueOf(chars);
    Log.d("@@@", x + "a " + result);
}

This one work when Java end data to C++ server. 当Java将数据结束到C ++服务器时,这一功能就起作用了。 I do this on C++ but not work on Java 我在C ++上执行此操作,但在Java上不起作用

Finally I try this, 最后我尝试一下

while((x = inr.read(chars, 0, chars.length - 1)) != -1){
    chars[x] = '\0';
    result += String.valueOf(chars);
    Log.d("@@@", x + "a " + result);
}

Or using StringBuilder dont work too. 或使用StringBuilder也不起作用。

After follow SM work, 遵循SM工作之后,

Log.d("@@@", "Waiting for server reply...");
            InputStream in = socket.getInputStream();
            InputStreamReader inr = new InputStreamReader(in);
            BufferedReader br = new BufferedReader(inr);

            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                Log.d("@@@", ": " + line);
                sb.append(line);
            }
            br.close();
            Log.d("@@@", ": " + sb.toString());

The output dont not pass thr the while loop 输出未通过while循环

05-31 11:04:37.734: D/dalvikvm(22624): GC_FOR_ALLOC freed 1621K, 39% free 15950K/25991K, paused 18ms, total 18ms
05-31 11:04:37.749: D/@@@(22624): Starting iMNSConnection...
05-31 11:04:37.749: D/@@@(22624): Is trying to connect to server...
05-31 11:04:37.759: D/@@@(22624): Waiting for server reply...
05-31 11:04:37.789: D/dalvikvm(22624): GC_CONCURRENT freed 1194K, 36% free 16744K/25991K, paused 1ms+1ms, total 13ms
05-31 11:04:37.789: D/dalvikvm(22624): WAIT_FOR_CONCURRENT_GC blocked 12ms
05-31 11:04:37.809: D/AbsListView(22624): Get MotionRecognitionManager
05-31 11:04:37.819: D/SensorManager(22624): unregisterListener::  Listener= android.view.OrientationEventListener$SensorEventListenerImpl@41d85420
05-31 11:04:37.819: D/Sensors(22624): Remain listener = Sending .. normal delay 200ms
05-31 11:04:37.819: I/Sensors(22624): sendDelay --- 200000000
05-31 11:04:37.819: D/SensorManager(22624): JNI - sendDelay
05-31 11:04:37.819: I/SensorManager(22624): Set normal delay = true
05-31 11:05:41.944: D/dalvikvm(22624): GC_CONCURRENT freed 1078K, 33% free 17590K/25991K, paused 21ms+20ms, total 82ms

Who produces the text Welcome !!! 谁产生文本Welcome !!! ? Maybe the other side doesn't close the stream. 也许另一侧没有关闭流。

In any case, your code is very ineffective. 无论如何,您的代码都是无效的。 You are creating many String objects, one per each character read. 您正在创建许多String对象,每个字符读取一次。 If you are reading text from stream, you'd better use something like: 如果要从流中读取文本,则最好使用类似以下的内容:

BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
    sb.append(line);
}
br.close(); // and catch exception

Java Strings are not null-terminated. Java字符串不是以Null结尾的。 If you receive x bytes into a byte[] buffer buffer , the correct way to construct a String from it is new String(buffer, 0, x) . 如果将x个字节接收到byte[]缓冲buffer ,则从中构造字符串的正确方法是new String(buffer, 0, x)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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