Hi,
I am having a problem reading data from a network. Here is the code of the thread that listening from a thread and saving the content as string.
private void ReadData()
{
string lMessageInString;
print("Reading data");
byte[] lInStream = new byte[1024];
char[] lCharStream = new char[1024];
serverStream = clientSocket.GetStream();
while (true)
{
print("While True");
serverStream.Read(lInStream, 0, lInStream.Length);
for (int i = 0; i < lInStream.Length; i++)
{
lCharStream[i] = (char)(((int)lInStream[i]));
}
lMessageInString = new string(lCharStream);
print("The message length: " + lMessageInString.Length);
ProcessMessage(lMessageInString);
print(lMessageInString);
}
}
The code seems to work but I have a problem trying to optimize the code. THe size of the string (which is derived from the array of char) is 1024 instead of the size of the character sent by the network. Using method serverStream.Length seems to fail the program. How can I limit the string length to match the size of the received characters from the stream? ie. if there are only 10 characters (bytes), the string will have 10 characters instead of the maximum 1024. It seems redundant to process the whole array when the stream only sent 10 characters.
Thank you in advance