gpt4 book ai didi

java - DataInputStream 的结果不一致

转载 作者:行者123 更新时间:2023-12-02 01:45:40 24 4
gpt4 key购买 nike

我在 Java 中使用 DataInputStream 和 DataOutputStream 时遇到了一个问题,如果没有一些非常愚蠢的措施,我就无法解决这个问题,并且想知道是否有人知道更好的方法(我非常确定有一个,但我还没有)找不到它)。

背景:我希望能够将 Json 字符串发送到服务器,并让服务器将该字符串解析为对象。这需要与语言无关,因为我正在研究的一个项目需要我有一个异构系统。

为了用最简单的示例来展示我的问题,我将排除 Gson/Json 等的创建,因为它可以使用任何字符串重新创建。代码如下:

public class ServerSide {
private final int PORT = 5001;
private ServerSocket servSock;
private Socket sock;
private DataInputStream dis;
private DataOutputStream dos;

public ServerSide() throws IOException {
servSock = new ServerSocket(PORT);
}

public void startListening() {
try {
sock = servSock.accept();
dis = new DataInputStream(sock.getInputStream());
byte[] bytes = new byte[1024];

dis.read(bytes);

String receivedMessage = new String(bytes);
System.out.println("Message received: " + receivedMessage);
sock.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

一个简单的测试类,它创建一个 ServerSide() 并在一段时间内调用 startListening(true)。

public class ClientSide {
private final int PORT = 5001;
private Socket socket;
private DataOutputStream dos;

public ClientSide() {

}
public void sendMessage(String m) {
try {
socket = new Socket("127.0.0.1", PORT);
dos = new DataOutputStream(socket.getOutputStream());
dos.writeBytes(m);
System.out.println("Message sent: " + m);

} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}

一个简单的测试类,创建 ClientSide() 对象并调用 sendMessage("this is a test message");

我遇到的问题是我只在服务器端收到部分消息。我不确定这是否是输入或输出流的问题,并且只能通过多次写入和读取数据并修剪之间的空白来找到解决方法。我的第一个问题是当尝试将 Json 字符串发送到我用 c# 编写的程序时,它总是会发送第一个字符 (“{”),然后在下一次读取时发送字符串的其余部分。我通过发送一个空格然后在服务器端忽略它来解决这个问题。当在第一次读取中读取看似随机数量的字符串时,在 java-java 上该问题会变得更糟。

上述代码在服务器端的一些示例输出是:

Message received: This is a tes

Message received: T

Message received: T

Message received: T

最佳答案

is.read(bytes); may return any number of bytes - 从 1 到 bytes.length

Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer. This method blocks until input data is available, end of file is detected, or an exception is thrown.

需要重复调​​用此方法,直到返回-1,这意味着流结束。

Reading from and Writing to a Socket提供了有关如何读取面向行的交换中的所有数据的示例。对于二进制数据(DataStream 实际上生成的数据),您需要使用 ByteArrayOutputStream、ByteArrayInputStream 和 DataInputStream 的组合:

InputStream sis = sock.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while (int len = sis.read(bytes) > 0) {
baos.write(bytes, 0, len);
}
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
DataInputStream dis = new DataInputStream(bais);
byte[] vBytes = new byte[baos.size()];
int sLen = dis.read(vBytes);
String receivedMessage = new String(vBytes, 0, sLen);
System.out.println("Message received: " + receivedMessage);
<小时/>

注意:上面的代码是回答特定问题。不要将其投入生产:)

关于java - DataInputStream 的结果不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53701535/

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