I need help figuring out how to fix the problem I'm observing with a large data channel over TCP using .NET sockets.
In short, when a client application starts, it connects to a specific port on the server. After connecting, the server starts sending data in real time to the client, which displays information in a user interface similar to a ticker. The server supports several client workstations, so data will be sent through several ports (several sockets).
Everything is implemented and works great with slow feed and low volume. I experience stress testing of the system to ensure stability and scalability. When I increase the frequency, the server is working fine. However, I see what seems like a lost package on clients. This happens at random times.
Currently, each transmitted message is preceded by a 4-byte value that determines the length of this message. When we receive data in the client, we add data to the buffer (stream) until we get so many bytes. Any additional bytes are considered the beginning of the next message. Again, this works fine until I increase the frequency.
In my test, I am sending a packet of about 225 bytes in size, followed by about 310kB and about 40kb more. Sending a message every 1 second works without fail with the launch of about 12 clients. Increasing the frequency to 1/2 second, I finally saw that one of the client displays freezes. The transition is 1/4 second and I can reproduce the problem with 4 clients in a few seconds.
Looking at my code (which I can provide, if necessary), I see that all clients receive data, but somehow the information fell "out of sync", and the expected length value is huge (in the 100 millionth range). As a result, we just keep reading the data and never perceive the end of the message.
I need either a better approach or a way to ensure that I get the expected data and do not lose packets. You can help?
UPDATE
I spent a ton of additional testing changing message size and delivery frequency. There is a certain correlation. The smaller the message sizes, the higher the frequency I can reach. But, inevitably, I can always break it.
So, to more accurately describe what I'm looking for, this is:
To understand what is happening. This will help me identify a possible solution, or at least set thresholds for reliable behavior.
Introduce a fail-safe mechanism, so when a problem occurs, I can handle this and possibly restore it. Perhaps adding a checksum to the data stream or something like that.
Here is the code that I run in client (receiving) applications:
public void StartListening(SocketAsyncEventArgs e) { e.Completed += SocketReceive; socket.ReceiveAsync(e); } private void SocketReceive(Object sender, SocketAsyncEventArgs e) { lock (_receiveLock) { ProcessData(e.Buffer, e.BytesTransferred); socket.ReceiveAsync(e); } } private void ProcessData(Byte[] bytes, Int32 count) { if (_currentBuffer == null) _currentBuffer = new ReceiveBuffer(); var numberOfBytesRead = _currentBuffer.Write(bytes, count); if (_currentBuffer.IsComplete) {