简体   繁体   中英

TCP Client sometimes merges data-packets

In a Unity-application I'm receiving like 160 JSON-Objects via TCP per second. My reading code below seems to struggle at such rates and a few times doesn't processData them as single messages. When trying to create a JSONObject ` instead I get something like this

improper JSON formatting:right","data":{"type":"flex","gesture":"open","data":[0.11425240454677936,0.11582253631723596,0.0947054436987323,0]}} {"type":"right","data":{"type":"sensor","data":[0.98638916015625,-0.0802001953125,0.08880615234375,-0.11248779296875,null]}} {"type":"right","data":{"type":"flex","gesture":"open","data":[0.11192072282133489,0.11739301138594425,0.09271687795177729,0]}} {"type":"right","data":{"type":"sensor","data":[0.98638916015625,-0.0802001953125,0.08880615234375,-0.11248779296875,null]}} {"type":"right",

This is my data processing function:

bool ProcessData()
{
    string temp = System.Text.Encoding.Default.GetString(readBuffer);
    //Debug.Log(string.Format("Client recv: '{0}'", temp));

    JSONObject obj = new JSONObject(temp);
}

Here's my TCP-code:

void connectToHub () {
    readBuffer = new byte[512];
    EndPoint endpoint;
    if (this.useUnixSocket == true) {
        endpoint = new UnixEndPoint(SOCKET_LOCATION);
    } else {
        endpoint = new IPEndPoint(this.ipAdress, this.port);
    }

    client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

    try {
        IAsyncResult result = client.BeginConnect(endpoint, 
                                    new AsyncCallback(this.EndConnect), 
                                    null);
        bool connectSuccess = result.AsyncWaitHandle.WaitOne(System.TimeSpan.FromSeconds(10));

        if (!connectSuccess){
            client.Close();
            Debug.LogError(string.Format("Client unable to connect. Failed"));
        }

    }
    catch(SystemException e) {
        Debug.LogError(string.Format("Client exception on beginconnect: {0}", e.Message));
    }

}

void EndConnect(IAsyncResult iar)
{
    client.EndConnect(iar);
    client.NoDelay = true;
    ReceiveData();
}


void ReceiveData()
{
    client.BeginReceive(readBuffer, 0, readBuffer.Length, SocketFlags.None, EndReceiveData, client);
}

void EndReceiveData(System.IAsyncResult iar)
{
    int numBytesReceived = client.EndReceive(iar);
    if (numBytesReceived > 0){
        this.ProcessData();

    }           
    // Continue receiving data
    ReceiveData();
}

It looks to me that at times you start processing the data before it has all arrived. You are calling EndReceiveData when you receive some data over TCP. If all the data fitted into the packet that has just arrived, you have no problem. If, however, the data is bigger than the packet, then you really should be reading all of the packets for that json object, before you start to parse it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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