gpt4 book ai didi

java - 通过 TCPSocket 将 Uint8List 从 dart 传递到 java

转载 作者:搜寻专家 更新时间:2023-11-01 03:15:40 24 4
gpt4 key购买 nike

我需要通过 TCPSocket 将 Uint8List 传递给 java 中的 byte[] 数组。两端大小不匹配。 PS:我是 Dart 的初学者。

我试过socket.add(buf), socket.write(buf), socket.writeAll(buf) 但都没有他们工作了

Flutter端代码(TCP客户端)

 void readVoiceData(Uint8List buf) {    
print("Send data size:"+buf.lengthInBytes.toString());
socket.add(buf);
}

OUTPUT: Send data size: 1280

Java 端的代码片段(TCP 服务器)

in = new DataInputStream(clientSocket.getInputStream());  
Log.d(TAG, "****Opened InputStream*******");
while (!isInterrupted()) {
if (mTrack != null) {
try {
in.read(bytes);
Log.d(TAG, "Received data size"+bytes.length);
}
}

OUTPUT: Received data size: 1

我确信套接字连接已正确建立,因为我能够通过它们完美地发送字符串和整数。

最佳答案

发生这种情况是因为 FilterInputStream(DataInput 是他的儿子)的 read() 方法读取 just the next avaliable byte .你,如果你想获得 InputStream 的总大小,请执行以下操作:

  size = in.available(); //returns an estimate of number of bytes avaliable on the stream
buf = new byte[size];
realLength= in.read(buf, 0, size);

这段代码摘自here .

关于java - 通过 TCPSocket 将 Uint8List 从 dart 传递到 java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54127741/

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