简体   繁体   English

通过Socket Java传输映像时丢失一个字节

[英]Missing one byte when transferring an image over Socket Java

I have a problem transferring a file over socket. 我在通过套接字传输文件时遇到问题。 I Wrote a simple client / server app and the client takes a screenshot and send it to server. 我编写了一个简单的客户端/服务器应用程序,客户端获取了屏幕截图并将其发送到服务器。 The problem is the file is not completed whatever i do, It's always missing the first byte from the array which makes the photo damaged. 问题是无论我做什么文件都没有完成,它总是缺少数组中的第一个字节,这使照片损坏了。 When I open the photo in any hex editor and compare the original photo with the one that the client sent, I can see the missing byte, as if I add it, the photo opens without the problem. 当我在任何十六进制编辑器中打开照片并将原始照片与客户端发送的照片进行比较时,我可以看到丢失的字节,就像我将其添加一样,该照片可以打开而不会出现问题。 The size of the sent file missing just one byte ! 发送文件的大小仅丢失一个字节! Here is a photo for the problem : 这是问题的照片:

Original photo 原始照片

原始照片

sent photo 已传送相片 已传送相片

Here is the code : 这是代码:

Server ( Receiver ) : 服务器(接收方):

byte[] buf;
InputStream inp;
try (BufferedOutputStream out1 = new BufferedOutputStream(new FileOutputStream(new File("final.jpeg")))) {
    buf = new byte[s.getReceiveBufferSize()];
    inp = new DataInputStream(s.getInputStream());
    Thread.sleep(200);
    int len = 0;
    while ((len = inp.read(buf)) >0){
    out1.write(buf,0,len);
    }
    out1.flush();
     inp.close();
     out1.close();
}

Client ( Sender ): 客户(发件人):

BufferedImage screenshot = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(screenshot, "jpeg", os);
ImageIO.write(screenshot, "jpeg", new File("test.jpeg"));
OutputStream out = new BufferedOutputStream( connection.getOutputStream());
out.write(os.toByteArray());
out.close();

I have tried to send the array with the same way I receive it but no lock. 我试图以与接收数组相同的方式发送数组,但没有锁定。 I have tried with, and without buffered, I have tried flush in both sides, I tried to turn off Nod antivirus, Tried a sleep when sending length, I almost tried everything without success . 我尝试过并且没有缓冲,尝试过两面冲洗,我试图关闭Nod防病毒软件,在发送长度时尝试睡眠,我几乎尝试了一切都没有成功。 I have tried on both, My pc and a virtual machine windows 7 ! 我已经尝试过,我的电脑和虚拟机的Windows 7! Any help will be appreciated. 任何帮助将不胜感激。

Edit : First 10 bytes from the original file : 编辑:从原始文件的前10个字节: 原版的

first 10 bytes from the sent file : 发送文件的前10个字节: 在此处输入图片说明

Please keep in mind that DataInputStream signals end of stream by returning value -1 from read() therefore your server reading loop should look like this: 请记住, DataInputStream通过从read()返回值-1来指示流的结尾,因此您的服务器读取循环应如下所示:

while ((len = inp.read(buf)) != -1){
    out1.write(buf,0,len);
}

Perhaps this helps. 也许这会有所帮助。

The code you posted does not lose data. 您发布的代码不会丢失数据。 Somewhere prior to executing the server code you posted, you have executed a single InputStream.read() of one byte, possibly in a misguided attempt to test for end of stream. 在执行发布的服务器代码之前的某个地方,您已经执行了一个字节的单个InputStream.read() ,这可能是在误导尝试测试流结束的尝试中。

The sleep is just literally a waste of time. 睡眠实际上只是在浪费时间。 Remove it. 去掉它。 You don't need the DataInput/OutputStreams either. 您也不需要DataInput/OutputStreams

The client code looks fine. 客户端代码看起来不错。 Must be the server. 必须是服务器。 You only posted the part when "some" input stream is written to a file. 仅在将“某些”输入流写入文件时才发布零件。 What happens before? 之前发生什么事? Anyone doing a read() on the input stream? 有人在输入流上执行read()吗?

Sorry for writing this in the "answer" section. 很抱歉将其写在“答案”部分。 Apparently, I cannot comment yet. 显然,我无法发表评论。

Ok it was my fault ! 好吧,这是我的错! I was looking for something wrong in server side but the fault was in client side ! 我在服务器端寻找问题,但故障在客户端! I opened a DataInputStream to read the order coming from server without closing it and that was the problem. 我打开了一个DataInputStream来读取来自服务器的订单而不关闭它,这就是问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM