How to read incoming TCP data packets by packets (i.e., without concatenation) ?

Dear all,

I’m actually sending data from Matlab to update a UNITY scene :

  • Matlab send “a” or “b” to UNITY, using TCP protocol
  • UNITY receive “a” or “b”, and update the color of an object based on this character

I’ve got an issue with the update of my variable called “coherence” in UNITY. When the script is running, packets of data coming from MATLAB are concatenating. For exemple, if I print “coherence” in the log file, it looks like “aaaaababbbbbbbbaaaaabbbbbbababababbbbaaababbaabbbaba”. What I would like, is my last packet of data to be the only one to be read and print (e.g, “coherence” is “a”, then “b”, then “b”, then “a” and so on, instead of being “a” then “ab” then “abb” then “abba” …).

Can you tell me if something is wrong in my code please ?

Thanks a lot for your input,

Yannick

// Use this for initialization of TCP Server
TcpListener listener;
String coherence;

// Initialization of the script
void Start()
{
_renderer = GetComponent();
listener = new TcpListener(55001);
listener.Start();
print(“is listening”);
}

void Update()
{

                        if (!listener.Pending())
                        {
                        }
                        else
                        {
                            print("socket comes");
                            TcpClient client = listener.AcceptTcpClient();
                            NetworkStream ns = client.GetStream();
                            StreamReader reader = new StreamReader(ns);
                            coherence = reader.ReadToEnd();
                            print(coherence);
                        }
                       

                        if (coherence == "a")
                        {
                            colorFin = Color.green;
                        }
                        else if (coherence == "b")
                        {
                            colorFin = Color.yellow;
                        }

}

You can not read TCP “packets” since TCP is not a packet protocol. It’s a data stream protocol. It creates a virtual “connection” and provides a bidirectional and endless stream of data until the connection is closed or dropped. So unless you build some kind of packet based protocol on top of TCP there is no concept of a packet on the application layer.

If you actually only add single characters / bytes to the stream you can of course just read them character by character (byte by byte).

Though your whole network code should be improved. First of all network communication should be handled in a seperate thread as it’s generally an asynchronous process. Second you only read “once” from a connected client until the stream buffer is empty and then you just drop the client. You usually have one thread to drive the listener to accept new clients and for each client you should have a seperate thread to handle the reading. To read single characters you may want to use Read instead of ReadToEnd.