gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:54:16 25 4
gpt4 key购买 nike

我写了这个程序,但它从未到达行b。我确定循环正在完成。有谁知道出了什么问题? :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

输出

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

已编辑

实际上我正在使用带有 Java 客户端的 C++ 服务器

C++ 方面

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

然后是客户端silde(Java)如果我使用 BufferedReader 并且什么也不打印。所以我用这个,

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

输出是这样的:

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)

所以我尝试添加这个

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

这一项在 Java 结束数据到 C++ 服务器时起作用。我在 C++ 上这样做,但在 Java 上不行

最后我试试这个,

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

或者使用 StringBuilder 也不行。

跟随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());

输出不通过 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

最佳答案

谁制作了文本 Welcome !!!?也许对方没有关闭流。

无论如何,你的代码是非常低效的。您正在创建许多 String 对象,每个读取的字符一个。如果你正在从流中读取文本,你最好使用类似的东西:

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 - 从 C++ 套接字读取 Java 中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16848880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com