简体   繁体   English

Android 插座 TCP 数据丢失

[英]Android Socket TCP Dataloss

I am unable to transmit an entire file using WiFi-Direct.我无法使用 WiFi-Direct 传输整个文件。 The file sender is indicating that the entire file has been copied over to the socket output stream.文件发送者指示整个文件已被复制到套接字 output stream。 The file receiver is only receiving roughly half of the file.文件接收器只接收大约一半的文件。

I looked at the contents of both the original file and the contents of the file storing the received data, and found the receiver is only receiving pieces of the original file.我查看了原始文件的内容和存储接收数据的文件的内容,发现接收方只接收原始文件的片段。 For example, it would receive bytes 0-100, and then it would jump to byte 245-350.例如,它将接收字节 0-100,然后跳转到字节 245-350。

Why is the receiver only receiving bits and pieces of the file, rather than the entire file?为什么接收方只接收文件的零碎部分,而不是整个文件?

File Receiving Logic文件接收逻辑

    private class FileReceiveThread(val channel: Channel) : TransmissionThread() {
        private var mFileName: String = ""
        private var mFileSize: Long = 0L
        private var mBytesReceivedTotal = 0L

        override fun run() {
            try {
                Timber.d("File receive thread running: fileSize=$mFileSize, fileName=$mFileName")
                val outputFile = File.createTempFile("file", "")
                val fileOutput = outputFile.outputStream()
                val channelInput = channel.getInputStream().unwrap()

                val inputBuffer = ByteArray(FILE_TX_BUFFER_SIZE)
                var bytesReceived = channelInput.read(inputBuffer)

                while (bytesReceived > 0) {
                    fileOutput.write(inputBuffer)
                    mBytesReceivedTotal += bytesReceived
                    Timber.d("Received $mBytesReceivedTotal total bytes")
                    bytesReceived = channelInput.read(inputBuffer)
                }

                onTransmitComplete?.invoke()
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }

        fun start(filename: String, size: Long) {
            mFileName = filename
            mFileSize = size
            start()
        }
    }

File Sending Logic文件发送逻辑

    private class FileSendThread : TransmissionThread() {
        var mFile: File? = null
        var mOutputStream: OutputStream? = null

        override fun run() {
            if (mFile != null && mOutputStream != null) {
                val inputStream = mFile!!.inputStream()
                val channelStream = mOutputStream!!
                val buffer = ByteArray(FILE_TX_BUFFER_SIZE)

                var bytesRead = inputStream.read(buffer)
                var totalBytesRead = 0L + bytesRead

                while (bytesRead > 0) {
                    Timber.v("Read $bytesRead, total $totalBytesRead")
                    channelStream.write(buffer)
                    bytesRead = inputStream.read(buffer)
                    totalBytesRead += bytesRead
                }

                Timber.d("Wrote file to output stream")

                inputStream.close()

                Timber.d("No more data to send")
                onTransmitComplete?.invoke()
            } else Timber.d("Parameters null: file=$mFile")
        }

        fun start(file: File, stream: OutputStream) {
            mFile = file
            mOutputStream = stream
            start()
        }
    }
           while (inputStream.read(buffer) > 0) {
                channelStream.write(buffer)
            }

The read() will often not fill the complete buffer. read() 通常不会填满整个缓冲区。 Hence if you write the buffer then only as far as it is filled.因此,如果您写入缓冲区,那么只要它被填充即可。

           var totalbytesread = 0;
           var nread;
           while ((nread = inputStream.read(buffer)) > 0) {
                channelStream.write(buffer, 0, nread)
                totalbytesread += nread;
            }

           channelStream.close()';

Log the totalbytesread.记录读取的总字节数。 Your original code would have caused a bigger received file so there is something else to be discovered..您的原始代码会导致接收到的文件更大,因此还有其他内容需要发现..

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

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