If you call the code without a pause several times, then the data from the last call gets into the socket.

_text="...";//Некие текстовые данные byte[] data = new byte[_text.Length]; data = Encoding.UTF8.GetBytes(_text); _socket.Send(data) 

How to avoid it without slowing down the flow through

 Thread.Sleep(1000); 
  • data = Encoding.UTF8.GetBytes(data); - is it generally compiled? Show your real code. - VladD
  • Here it is. Corrected ... - jshapen
  • one
    And byte[] data you have a local variable, or field? Give a problem reproducing example. The minimum reproducible example is VladD
  • Local variable. I can not give an example. There a lot of different software is used. It is simply incomprehensible to me why everything is in order with a one-time call and problems with multiple calls. - jshapen
  • one
    @jshapen naturally does not affect, since you do not use this array, but the length of the text and the number of bytes are not the same - Primus Singularis

1 answer 1

The fact is that sockets are unpredictable things, the unknown may be ping between clients and the server.

I would advise you to send first the size, then the data or the sign of the end of the line (Markers).

It is better to use UDP, because TCP is a stream, in which data rushes in, and then just sent. I can still advise you to write your protocol on top of TCP, but this is a big meal ...

New member
Matvey is a new member of the site. Be lenient in asking clarifying questions, commenting and answering. Read about the norms of behavior .