How should I handle incomplete packet buffers?

I am writing a client for a server that usually sends data as strings of 500 or less bytes. However, data will sometimes exceed this, and one data set can contain 200,000 bytes, as all clients know (during initialization or significant events). However, I would not want each client to work with a 50 MB socket buffer (if possible).

Each data set is separated by a null character \0. What structure should I look for to store partially sent datasets?

For example, a server might send ABCDEFGHIJKLMNOPQRSTUV\0WXYZ\0123!\0. I would like to handle ABCDEFGHIJKLMNOPQRSTUV, WXYZand 123!whatever. In addition, the server can send ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890LOL123HAHATHISISREALLYLONGwithout a trailing character. I would like this dataset to be stored somewhere for later addition and processing.

In addition, I use the techniques of asynchronous socket ( BeginSend, EndSend, BeginReceive, EndReceive), if that matters.

I am currently discussing between List<Byte>and StringBuilder. Any comparison of the two for this situation would be very helpful.

+3
source share
4 answers

List<byte> , .NET , . , List.RemoveRange(), .

, , , , .

( Ben S, , - , StringBuilder . , .)

+2

. , .

, TCP - , . , , .

, , , . , , , , - , , , .

, - "" , .

+4

I would just use StringBuilderand read one character at a time, copying and emptying the builder whenever I click the terminator zero.

+1
source

I wrote this answer regarding Java sockets, but the concept is the same.

What is the best way to control a socket for new data and then process that data?

0
source

Source: https://habr.com/ru/post/1736930/


All Articles