The TCPClient documentation from Microsoft is actually pretty useful.
How do read the data from the TCPClient
The key part in the Microsoft example for reading the data from the socket is this:
// Get the stream
NetworkStream stream = client.GetStream();
Byte[] data = new Byte[256];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
int bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
They use the GetStream method of the TCPClient class and read all the available data (up to 256 bytes) from the stream.
This is slightly cheating though, as they are just receiving one lot of data and assuming it is a complete and correct message. It will work fine so long as your client is 100% predicable in that it always sends the same thing then disconnects.
Better way of handling this below...
How do you know if a message is received?
You don't, you just keep receiving data until there is no more then check what you have received (or check as you go).
To do this, you have to design the message to include some way of telling the length (integer at the start indicating length), or have some end of message marker (e.g. carriage return) or have a fixed length message.
For example your message specification might be like this:
DATA
hello friend
END
By looking for END followed by a carriage return you will know you have found the end of the message. Likewise the DATA
marker at the beginning helps you to tell where a message starts and split out the message data.
What is the best way to read this data?
All data sent/received will be in binary, you need to Encode and Decode the data. In the example, they are converting binary data to string (and the other way round) using the built in ASCII Encoding class GetString and GetBytes methods.
Consider implementing a known protocol
I would recommend to (where possible) use an established protocol rather than just sending plain text over TCP. There are standards for almost anything and anyone connecting to your application will find it easier to follow a known and defined standard.
There are also many pre-built libraries for commonly used protocols saving you and your clients the hastle.