I have a TCP connection and a StreamWriter.
public void SendData(string data, TcpClient client)
{
StreamWriter writer = new StreamWriter(client.GetStream());
writer.WriteLine(data);
writer.Flush();
}
When I call SendData() subsequently like this
SendData("hi", client);
SendData("how are you", client);
SendData("?", client);
the client only receives the first line. I added a Thread.Sleep(100) to the SendData() method like this:
public void SendData(string data, TcpClient client)
{
StreamWriter writer = new StreamWriter(client.GetStream());
writer.WriteLine(data);
writer.Flush();
Thread.Sleep(100);
}
The second case works perfectly fine. Do you think my solution is acceptable or should i try to change the client? Do you have any experience or ideas about the origin of this problem?
The client is listening in Unity in a function called Update(), which is being called every frame. If you send twice the second data will be sent between two frames and can't be received. The thread.sleep is necessary.