C #: How to handle TCP socket buffer data? How are bytes or converted to ascii string?
C #: How to handle TCP socket buffer data? How are bytes or converted to ascii string?
I am using methods that are involved in parsing specific data from the returned tcp socket buffer, and what would be the best practice?
Should I process / parse my data as a raw byte? Or should I process it after converting it to ascii string since string data types have shorter text operations?
In general, like bytes. It's basically binary data - it's up to the protocol above TCP to interpret this binary data appropriately.
Now what is your data? Are you in control of the protocol? If so, is it text data? Converting binary data for an image (for example) to ASCII is likely to be disastrous ... but if it really is an ASCII protocol, this is probably the right way to go.
If you do not know the protocol, do not perform any conversions: they can lose information if you are not very careful (for example, use base64 instead of ASCII encoding).
If you know the protocol, that should dictate how you process the data.
a source to share
I think it depends on the data contained in the buffer. If it is a string, it is convenient to convert the buffer to a string.
Also note that you should add some header data before sending the string. Like data payload length, checksum / parity, etc. The network is a non-deterministic environment and you cannot tell exactly who is sending what on the specified port, you can get crashes if you just convert the received buffer directly to a string.
a source to share