Neither .NET nor the TCP protocol have anything built in to define the size of the message to come in advance. The TCP protocol only specifies that all data will be transferred to the receiving end point (or at least that the best effort will be employed to do so).
You are solely responsible for defining a way to let the receiver know how much data to read. The details of how you do this are - as others have pointed out - dependent of the nature of what you're transferring: you could send the length first like you mentioned, you could encode special sequences called terminators, you could use predefined data chunks so all messages have the same size, etc.
EDIT
This started out as a comment but there's more to it than fits that limit.
To add NULL to the stream simply means appending a character which has the binary value 0 (not to be confused with the character 0
). Depending on the encoding you're using for your transfer (i.e. ASCII, UTF-8, UTF-16 etc) that may translate into sending one or more 0 bytes but if you're using appropriate translation you simply need to put something like \0
in your string. Here's an example:
string textToSend = "This is a NULL Terminated text\0";
byte[] bufferToSend = Encoding.UTF8Encoding.GetBytes(textToSend);
Of course all of the above assumes that all of the rest of the data you're sending does not contain any other NULLs. That means that it's text, and not arbitrary binary data (such as the contents of a file). That's very important! Otherwise you can't use NULL as a message terminator and you have to come up with another scheme.