【问题标题】:BufferInputStream not reading the last packetBufferInputStream 没有读取最后一个数据包
【发布时间】:2016-12-18 04:35:47
【问题描述】:

我正在尝试将 apk 文件从一台设备发送到另一台设备。基本上,要发送的用户有一个带有他安装的应用程序(非系统应用程序)的网格视图,当用户按下其中一个应用程序的图标时,它会将其发送到另一台设备。该文件被缓冲成更小的数据包。

我正在做一些系统输出来检查正在发生的事情,同时发送方设备正在发送 X 个数据包,而接收方接收的数据包少于 X 个数据包(这取决于正在发送的应用程序,所以这就是我写 X 的原因) .我认为这是最后一个丢失的。

我的缓冲区的长度是 4096,而“最后一个数据包”只有 2115。在接收方,它不会抛出读写文件后的系统输出。所以程序卡在阅读指令上。 我的问题是为什么最后一个数据包没有被传输?

这是我的代码。

客户端 - apk 的发送者。

     public class SendThread implements Runnable{

    @Override
    public void run() {
        try {

            if(clientSocket==null){
                clientSocket=new Socket(serverIP,porto);
                System.out.println("client comm - Created the client socket..");
            }

            System.out.println(" client comm, i am here in the send thread, going to create the outputstream object");
            if(out==null){
                out = new ObjectOutputStream(clientSocket.getOutputStream());
                System.out.println(" Client COMM SEND THREAD - JUST created the outputstream .");
            }


            File apkToSend=new File(filePath);
            byte[] buffer = new byte [4096];
            BufferedInputStream bis=new BufferedInputStream(new FileInputStream(apkToSend));
            int count;
            int total=0; 
            while((count=bis.read(buffer,0,4096))!=-1){
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                out.write(buffer,0,count);
                total+=count;
                out.flush();
                System.out.println("Client Comm send thread - already sent this ammount : "+total);
            }

            bis.close();
            out.flush();
            System.out.println("clientComm send thread - Just sent the message ! i am after the out.writeOBject. Not saving it to a string yet.");


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

现在是服务器端 - 接收文件的设备..

    public class ReceiveThread implements Runnable{

    @Override
    public void run() {
        try {
            if(socket==null){
                socket=serverSocket.accept();
                System.out.println("serverComm - accepted the socket from the client.");
                out=new ObjectOutputStream(socket.getOutputStream());
                out.writeObject("Hello client, i the server, sending you this message because i want to. SO that you unlock from the input stream creation");
                System.out.println("SErverComm Created the output stream and sent a HEllo message to the client ");
            }

            if(in==null){
                in = new ObjectInputStream(socket.getInputStream());

            }
            while(!serverSocket.isClosed()){
                try {
                    if(!handshakeDone){
                        System.out.println("server comm receive thread - INSIDE THE WHILE CICLE, before the in.readObject");
                        String message = (String) in.readObject();
                        System.out.println("ServercomM receive thread - just did the handshake message control");
                        handshakeDone=true;
                    }else{

                        System.out.println("server comm receive thread - INSIDE THE WHILE CICLE, before the in.readObject");
                        File apkReceived = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/testeReceiveServerComm.apk");
                        byte[] buffer = new byte [4096];
                        BufferedInputStream bis=new BufferedInputStream(in);
                        FileOutputStream fos=new FileOutputStream(apkReceived);
                        int count=0;
                        int total=0;
                        while((count=bis.read(buffer,0,4096))!=-1){

                            fos.write(buffer,0,count);
                            total+=count;
                            System.out.println("Server Comm receive thread - already received this ammount : "+total);

                        }
                        ;
                        System.out.println("Already received everything ! ");
                        fos.flush();
                        bis.close();
                        fos.close();
                        System.out.println(" server comm receive thread - already read the object !!!!!!!!!!!!!!");

                    }

                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

【问题讨论】:

  • 没有理由使用 BifferInputStream,因为您一次读取 4092 个字节来缓冲自己。您可以尝试将其提高到 8192,但无需双缓冲。

标签: android sockets io buffer file-transfer


【解决方案1】:

这里有很多谬误。

  1. 发送方设备同时发送X个数据包,接收方接收不到X个数据包

    你没有计算数据包,所以你不可能知道这一点。在任何情况下,接收到的“数据包”的数量与 TCP 中发送的数量无关。重要的是 字节 的数量,这里没有证据表明您完全丢失了任何字节。您所看到的是,当您的缓冲区大小为 4096 时,最后一次读取返回 2115 个字节。这意味着输入文件的大小是 N*4096+2115 而不是 4096 的倍数。

  2. 您正在使用ObjectInputStream 和ObjectOutputStream,但您只发送字节。您可以为此使用套接字自己的输入和输出流:无需支付字节流的额外开销。

  3. 你毫无意义地包装了BufferedInputStream *aroundtheObjectInputStream`。应该反过来。
  4. 旅游代码中的Thread.sleep() 简直是在浪费时间。和空间。删除它。
  5. 在套接字已经关闭后刷新输出流。您应该关闭 输出 流,而不是输入流,关闭它会使刷新变得多余。
  6. 您还必须捕获并忽略由flush() 产生的IOException,除非您非常幸运并且没有什么可刷新的。我不明白这是怎么可能的,因为您的文件不是缓冲区大小的倍数。
  7. 您正在使用while (!serverSocket.isClosed()) 作为从接受的套接字读取的终止测试。这毫无意义。
  8. 您的接收线程同时接受和处理单个连接。这使您的整个服务器成为单线程的。应该有一个用于接受的线程,以及用于处理每个接受的套接字的线程。

您需要仔细阅读网络教程,例如 Oracle Java 教程的自定义网络部分。这不是编写网络代码的方法。

【讨论】:

  • 我有那些 thread.sleep 是因为有人告诉我在 stackoverflow 上这样做。我试过了,但显然它没有用。您 7. 中的条件只是为了让我可以接收多个文件,并且我总是在听一些东西。如果我没有这种情况,服务器将如何一直在监听?另外,我没有关闭套接字..
  • 服务器将始终在单独的接受线程中进行侦听。您在提到的循环中所做的是从您已经接受的套接字中读取数据。它与ServerSocket 没有任何关系。在网络代码中休眠实际上是在浪费时间。而且您的服务器不会“继续收听”。它只处理一个连接。除非您执行上述 (8)。
  • 但是如果我执行这个建议,我应该什么时候开始接收线程?我无法预测何时接收数据,这就是为什么我认为接收某些东西的线程应该始终是“活动的”
  • 您应该在接受套接字后立即启动套接字的接收线程。这在我见过的几乎所有网络教程和示例中都有体现,无论如何我认为这很明显。
  • 好的,当我发布这个时我有点困惑,而且我帖子中的一些代码甚至在你回答之前就已经改变了。我有点为上面的一些东西感到羞耻。谢谢您的意见。你已经在其他问题上帮助了我。我还是不明白为什么我不应该使用 ObjectOutputStream... 我发送一个初始字符串只是为了向双方打个招呼,然后我发送一个 apk 文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多