我的应用程序没有从设备接收整个数据

时间:2017-10-31 08:42:09

标签: android buffer

在我的应用程序中,我需要从自定义设备接收数据并将其保存在文本文件中。数据通过AP(套接字)发送。连接正常,创建文件正常,写入数据也没问题。但是,我没有收到整个数据。例如,文件正在以一半的方式关闭,我丢失了一半的数据。可能是什么问题呢 ?缓冲区的大小没问题。

这是我的代码:

private void synchData() {

        this.serverThread = new Thread(new ServerThread());
        this.serverThread.start();
    }

    class ServerThread implements Runnable {

        @Override
        public void run() {
            Socket socket = null;
            try {
                serverSocket = new ServerSocket(SERVERT_PORT);
                Log.e(TAG, "synchData0: ");

            } catch (IOException e) {
                e.printStackTrace();
            }
            while (!Thread.currentThread().isInterrupted()) {
                try {
                    if (serverSocket != null) {
                        socket = serverSocket.accept();
                        CommunicationThread commThread = new CommunicationThread(socket);
                        new Thread(commThread).start();
                    }

                } catch (IOException e) {
                    Log.e(TAG, "run: " + e.getMessage());
                }
            }
        }
    }

    class CommunicationThread implements Runnable {

        private Socket clientSocket;
        private BufferedReader input;

        byte[] bytes = new byte[16 * 1024];

        public CommunicationThread(Socket clientSocket) {
            this.clientSocket = clientSocket;
            try {
                InputStream in = this.clientSocket.getInputStream();
                File root = new File("/sdcard/mente/" + user.getID() );
                if(!root.exists())
                    root.mkdirs();
                String fileName = new SimpleDateFormat(Helper.DATE_FORMAT, Locale.ENGLISH).format(new Date()) + ".txt".replace(" ","_");
                file = new File(root,fileName);
                OutputStream out = new FileOutputStream(file);
                int count;
                StringBuilder sb = new StringBuilder();

                while ((count = in.read(bytes)) > 0) {
                    syncProgressDialog.show();

                    out.write(bytes, 0, count);
                    for (byte b : bytes) {
                        sb.append(String.format("%02X ", b));
                    }
                    Log.e(TAG, "CommunicationThread: " + sb.toString() );
                    if (sb.toString().contains("3C 65 6E 64 5F 72 61 77 3E 74 72 75 65 3C 2F 65 6E 64 5F 72 61 77 3E") && syncProgressDialog != null) {
                        syncProgressDialog.dismiss();
                    }
                }
                this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream(), Charset.forName("ISO-8859-2")));

            } catch (IOException e) {
                Log.e(TAG, "error: " + e.getMessage());
            } catch (Exception e) {
                e.printStackTrace();
            }
            syncProgressDialog.dismiss();

        }

0 个答案:

没有答案