java - TCP 文件传输中缺少字节

标签 java tcp

我需要能够读取文件,将其分成任意大小的数据包,比如 512 字节,然后通过 TCP 发送这些数据包。问题是,接收方没有收到我发送的所有字节。如果我发送 1000 个数据包,接收方在从 InputStream 读取时会阻塞,因为他没有更多数据可以读取大约 990 个数据包。

这是代码(只是发送和接收部分):

发件人:

int parts = (int)Math.ceil((double)(file.length()/512.0));

out.println(parts+"");
int readFile;
int i = 0;
while ((readFile = fileIn.read(buffer)) != -1) {
   i++;
   fileOut.write(buffer, 0, readFile);
   fileOut.flush();
   System.out.println("-- Sent packet " + i + "/" + parts + ". " + "Bytes sent = " + readFile);
}

接收者:

int parts = Integer.parseInt(in.readLine());
byte[] buffer = new byte[512];
FileOutputStream pw = new FileOutputStream("file.ext");
DataInputStream fileIn = new DataInputStream(socket.getInputStream());
for(int j = 0; j < parts; j++){
   int read = 0;
   if(j == parts - 1){
      read = fileIn.read(buffer);
      pw.write(buffer, 0, read);
   }else{
      fileIn.readFully(buffer);
      pw.write(buffer);
   }
   System.out.println("-- Received packet " + (j+1) + "/" + parts + ". Read " +read+ " bytes.");
}

我尝试增加套接字的发送和接收缓冲区大小但没有成功。我错过了什么?

这是一个输出示例:

发件人:

-- Sent packet 1/10. Bytes sent = 512
-- Sent packet 2/10. Bytes sent = 512
-- Sent packet 3/10. Bytes sent = 512
-- Sent packet 4/10. Bytes sent = 512
-- Sent packet 5/10. Bytes sent = 512
-- Sent packet 6/10. Bytes sent = 512
-- Sent packet 7/10. Bytes sent = 512
-- Sent packet 8/10. Bytes sent = 512
-- Sent packet 9/10. Bytes sent = 512
-- Sent packet 10/10. Bytes sent = 234

接收者:

-- Received packet 1/10. Read 512 bytes.
-- Received packet 2/10. Read 512 bytes.
-- Received packet 3/10. Read 512 bytes.
-- Received packet 4/10. Read 512 bytes.
-- Received packet 5/10. Read 512 bytes.
-- Received packet 6/10. Read 512 bytes.
-- Received packet 7/10. Read 512 bytes. (And it blocks here, because there is no more data to read)

最佳答案

TCP 是一种流协议(protocol)。没有数据包,只有一个数据流。

您不应该假设单个 write() (有或没有 flush() )将对应于单个 read() .因此你的接收循环 for(int j = 0; j < parts; j++)被误导了:更好的方法是计算读取的字节数量,并为read()做好准备。调用返回不同数量的数据。

在评论中你认为 readFully()解决了这个问题。然而,我关心的与其说是代码,不如说是流的基于数据包的 View 。这会导致在您的最终 fileIn.read(buffer) 中出现这样的错误称呼。它可能会返回作为您上一个“数据包”的一部分发送的数据的一半,而您永远不会知道!

关于java - TCP 文件传输中缺少字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8370268/

相关文章:

java - 任何不良使用界面的例子?

java - 如何在java中读取对象(java.lang.Object)

networking - TCP 是否从一个间隔中随机选择一个时间来决定何时发生超时?

c - 从 TCP/IP 流量构建文件?

tcp - 如何保持一百万个并发 TCP 连接?

java - 将对象集合序列化为 JSON 和 XML

java - 在java中使用primefaces显示Blob图像

java - ActionListener 不绘图,也不调用函数

.net - TcpListener 请求泛滥

android - 如何在 android 中为 tcp 套接字强制发送 RST 而不是 FIN