N
N
Noortvel2019-10-06 19:56:28
C++ / C#
Noortvel, 2019-10-06 19:56:28

Is it possible to get rid of "sticking" of bytes during WriteAsync from TcpClient.GetStream()?

There is a TcpClient object, NoDelay = true is set, I write an array of 16 bytes asynchronously to its stream, but when I send 2 arrays almost in a row, they stick together and at the other end, when reading, 32 bytes are obtained. How to fix it?

byte[] b1 = GetSomeBytesLen16();
client.GetStream().WriteAsync(b1, 0, b1.Length);
SomeAction1();
SomeAction1();
byte[] b2 = GetSomeBytes2Len16();
client.GetStream().WriteAsync(b2, 0, b2.Length);

//Где то в колбеке от BeginRead
int count = client.GetStream().EndRead(ar);//count = 32

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey, 2019-10-06
@Noortvel

No way. TCP is a pipe of bytes. You must separate the byte stream into packets yourself.

L
LiptonOlolo, 2019-10-07
@LiptonOlolo

Make it simple, create a specific structure for your data. But at the very beginning, allocate 4 bytes for SIZE, for example, you have an array of bytes and 100 values, write 100 to the beginning of the stream and read exactly 100 bytes on the server / client, profit

C
cicatrix, 2019-10-07
@cicatrix

Data groups must be separated. How to do it is your task. Typically, a terminating character (signature) is used, for example \0, if the length of the transmitted data is not known in advance, or a character (signature) of the beginning of new data, followed by the length of the transmitted block is usually sent.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question