'Exchanging small chunks of data over socket in Android

I need to receive small portions of data (100-300 bytes) from the server. To do this, I use a BufferedInputStream with bufferSize = 256. Sometimes the data arrives with a very long delay.

socket.getInputStream().buffered(256)?.use {inputStream->
            do {
                val byteArray = ByteArray(4096)
                var count = -1
                try {
                    count = inputStream.read(byteArray)
                }
                catch (ex : Exception){
                    ex.printStackTrace()
                }
                if(count==-1){
                    break
                }
            }while (count>-1 && isConnected())
        }


Solution 1:[1]

I found a solution for my problem. I set

socket?.receiveBufferSize = 512
socket?.tcpNoDelay = true

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dr_yand