I wrote a server-client communication with TCP sockets on Windows and it works properly, but now i'm trying to port the client-side to Windows Phone, but I'm really stuck at data receiving. I'm using StreamSocket and with that I need to know the length of the data. For example:
DataReader dataReader = new DataReader(clientSocket.InputStream);
uint bytesRead = 0;
bytesRead = await dataReader.LoadAsync(SizeOfTheData); // Here i should write the size of data, but how can I get it?
if (bytesRead == 0)
return;
byte[] data = new byte[bytesRead];
dataReader.ReadBytes(data);
I tried to do this on server-side, but I don't think this is a good solution:
byte[] data = SomeData();
byte[] length = System.Text.Encoding.ASCII.GetBytes(data.Length.ToString());
// Send the length of the data
serverSocket.Send(length);
// Send the data
serverSocket.Send(data);
So my question is, how can I send the length and the data in the same packet, and how can I properly process it on client-side?