I have been working on a project where I send strings from a client to a server in C#. My questions are the following, if some of you have the time to answer:
I send my string as following:
clientStream.Write(buffer, 0, buffer.Length); clientStream.Flush();
where clientStream is a NetworkStream. But it becomes a problem if the functions are in a while loop as following:
while(true)
{
clientStream.Write(buffer, 0, buffer.Length);
clientStream.Flush();
}
The messages seems to sometimes be intertwined and corrupt at the server part. But not if I add a Thread.Sleep (with perhaps 30 as an in parameter) call in the while loop. I wonder why the messages gets intertwined if it's an TCP socket connection? How does the function calls:
clientStream.Write(buffer, 0, buffer.Length);
clientStream.Flush();
work?
- How many messages can you send per second via a TCP socket connection? What does the speed depend on? Thank you for your time!
Writewill not necessarily result in two calls toReadon the server. The data may be split or combined at any point. It will always be sequential, however.buffer, and how you're reassembling the messages on the server. (And how you determine when a message starts and ends.)