Why does my file, sent through TCP, contain more data than the file it self contains?

huangapple go评论127阅读模式
英文:

Why does my file, sent through TCP, contain more data than the file it self contains?

问题

我一直在尝试通过TCP向我的一位同事发送文件,并检查文件是否正确到达。我们已经成功地发送了包含一些文本的简单.txt文件,但是有些地方似乎不对劲。

每当通过txt发送消息时,文件本身包含的内容似乎比原始消息多?
例如,我的同事发送带有内容123456789012345678901234567890123.txt文件。我们使用DataOutputStreamFileInputStream进行发送,并使用文件长度(file.length)进行动态文件大小检查。

动态文件大小变量被传递给byte[] buffer = new byte[filesize]

最终我们使用以下方式发送:

while (fis.read(buffer) > 0) {
    dos.write(buffer);
}

fis.close();
dos.close();    

使用这种方法得到以下结果:

发送: 123456789012345678901234567890123
接收: 12345678901234567890123456789012378

正如可见,出现了78被粘贴在消息后面的情况,我们一直没有弄清楚发生了什么,想知道到底出了什么问题。

更奇怪的是,在尝试了一些次数后,发送的消息被正常接收,没有额外的杂质?这非常不规则。

非常感谢任何提供的帮助,谢谢!

英文:

I have been trying to send a file through TCP to one of my colleagues and check if the files arrive correctly. We've succeeded in sending simple .txt files with some text in it, but there's something off.

Whenever a message is sent through txt, the file itself contains more than the original message?
e.g my colleague sends the .txt file with the content 123456789012345678901234567890123. It gets sent using DataOutputStream and FileInputStream with a dynamic filesize check using file.length.

the dynamic filesize variable gets fed to the byte[] buffer = new byte[filesize]

We eventually sent it using

while (fis.read(buffer) > 0) {
        dos.write(buffer);
    }
    
    fis.close();
    dos.close();    
}

Using this approach yields the following result:

sent:     123456789012345678901234567890123
received: 12345678901234567890123456789012378

As is visible, for some reason 78 gets pasted behind the message, we haven't been able to figure out what's going and we were wondering what was going on.

What's even weirder is that after some more tries, the sent messages are received as is/ arrived without any extra giberish? This is very irregular.

Any input is greatly appreciated, thank you!

答案1

得分: 2

    while (fis.read(buffer) > 0) {
        dos.write(buffer);

你假设read方法填满了整个缓冲区。大多数情况下,它可能确实如此。但有时候,只有缓冲区的一部分被读取。

根据javadoc

> 返回读入缓冲区的总字节数,如果没有更多数据,因为已经到达流的末尾,则返回-1。

因此,read()方法告诉你读取了多少字节。你必须确保继续读取,直到获得了你需要的全部字节!否则,你将使用只被部分填充了新数据的缓冲区。


<details>
<summary>英文:</summary>

    while (fis.read(buffer) &gt; 0) {
        dos.write(buffer);

You assume that the read populated the complete buffer. Most often, it might do that. But sometimes, only **parts** of the buffer are read. 

From the [javadoc][1]:

&gt; **returns** the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.


Therefore, that `read()` method tells you how many bytes got read. You have to ensure keep reading until you got exactly your bytes together! Otherwise, you will use a buffer that has only been *partially* filled with new data.


  [1]: https://docs.oracle.com/javase/8/docs/api/java/io/InputStream.html#read-byte:A-

</details>



huangapple
  • 本文由 发表于 2020年10月20日 21:30:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64446218.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定