gpt4 book ai didi

java - istream.write 之后的套接字 DataInputStream 和 EOF 标志 : Is there a "conflict" when writing with istream. writeUTF?

转载 作者:行者123 更新时间:2023-11-30 06:13:39 25 4
gpt4 key购买 nike

我正在编写一个程序在两台计算机之间传输文件,但我没有关闭套接字,因为我正在使用同一套接字传输其他消息(例如传输服务器的文件夹组织)。

在某个时刻,我有以下代码将文件从服务器传输到客户端:

服务器

  System.out.println("Sending " + threadItem.second.getName() + " of " + threadItem.second.length() + "b");

FileInputStream fileInputStream = new FileInputStream(threadItem.second);

byte[] buffer = new byte[BLOCK_SIZE];

int count;
while ((count = fileInputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, count);
}

outputStream.write(END_OF_STREAM, 0, 1);

System.out.println("File sent.");

fileInputStream.close();

并且客户端收到:

  FileOutputStream fOutputStream = new FileOutputStream(new File("./received/" + obj.toString()));
byte[] buffer = new byte[4096];
int count;

while ((count = dataInputStream.read(buffer)) > 0 && !(buffer[count - 1] == '\0')) {
fOutputStream.write(buffer, 0, count);
}

fOutputStream.close();

它有效,但真正的问题是当我在上述情况发生后立即从服务器向客户端发送另一条消息时:

服务器

outputStream.writeUT(new JSONObject()
.put("command", MessageHandler.ConnectionMessage.OVER.toString())
.toString());

客户端使用以下方式接收:

JSONObject jsonObject = new JSONObject(dataInputStream.readUTF());

但是,在上面这一行我得到:

SEVERE: null
java.io.UTFDataFormatException: malformed input around byte 1
at java.io.DataInputStream.readUTF(DataInputStream.java:656)
at java.io.DataInputStream.readUTF(DataInputStream.java:564)
at labrpc.secondquestion.Client.lambda$jButton3ActionPerformed$1(Client.java:499)
at java.lang.Thread.run(Thread.java:748)

不知何故write损坏了writeUTF或类似的东西(我一直在搜索与之相关的所有内容,但没有发现任何相关内容),我尝试删除 write从服务器只是为了测试,这些错误没有出现,但这是一个重要的例程。

我的问题是:在套接字通信中 write 和 writeUTF 是否是互斥的?

提前致谢。

最佳答案

我按照 JB Nizet 的提示解决了这个问题

由于我已经知道了文件的长度,我发现我可以将长度发送给客户端,因此它只能迭代指定的长度,通过长度推断文件的虚拟结尾并已读取字节。

服务器端: FileInputStream fileInputStream = new FileInputStream(threadItem.second);

 progressListener.dmaSend(threadItem.second.getName(), length);

byte[] buffer = new byte[BLOCK_SIZE];

for (int curLength, accumulator = 0; accumulator < length; outputStream.write(buffer, 0, curLength)) {
System.out.println("Reading part [" + accumulator + "," + length + "]");
accumulator += (curLength = fileInputStream.read(buffer));
System.out.println("Sending part [" + accumulator + "," + length + "]");
}

System.out.println("File sent.");

客户端:

 FileOutputStream fOutputStream = new FileOutputStream(new File("./received/" + obj.toString()));
byte[] buffer = new byte[BLOCK_SIZE];

for (int curLength, accumulator = 0; accumulator < length; fOutputStream.write(buffer, 0, curLength)) {
System.out.println("Reading part [" + accumulator + "," + length + "]");
accumulator += (curLength = dataInputStream.read(buffer));
System.out.println("Sending part [" + accumulator + "," + length + "]");
}

fOutputStream.close();

因此,可以在服务器上写入(套接字输出)/读取(文件输入),并在客户端上写入(文件输出)/读取(套接字输入)。

关于java - istream.write 之后的套接字 DataInputStream 和 EOF 标志 : Is there a "conflict" when writing with istream. writeUTF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49707455/

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