Why TCP Client splits data on Android 8.0.1?-ThrowExceptions

Exception or error:

I’m developing an app that sends data over a WiFi intranet connection and all the data is sent in one package on Android 5 but in several packages in Android 8.0.1

So for the code below in Android 5 I get “This is a test” and in Android 8.0.1

“T” (one packet)

“hi”

” i”

“s”

” a”

” test”

Does anybody would know what is happening?

This is part of the code

if(!Connected)
{
    //myClient.addListener();

    myClient = new Socket(serverName, port);
    myClient.setTcpNoDelay(true);
    //myClient.on("new message", onNewMessage);

    Connected=true;
    inFromServer = myClient.getInputStream();
    in = new DataInputStream(inFromServer);
}

if(Connected)
{
    //updateText.setText("Just connected to " + client.getRemoteSocketAddress());
    OutputStream outToServer = myClient.getOutputStream();
    DataOutputStream out = new DataOutputStream(outToServer);

    if(msgToSend.isEmpty())
    {
        out.writeChars("This is a test\r\n");
        //out.writeBytes("This is a test\r\n");
    }
    else
    {
        out.writeChars(msgToSend);
        //out.writeBytes(msgToSend);
    }
    out.flush();

    while(waitingData)
    {
        if(in.available()>0)
        {
            XChar = in.readByte();

            if(XChar>=32)
            {
                inBuffer+= Byte.toString(XChar);
            }
            else
            {
                waitingData= false;
            }
        }
    }
}
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *