I have answered my question but I'll post it here for anyone who is curious, it is impossible to read the length of the data without any secondary information being sent, however it is possible to load chunks off the stream until the stream is empty, this is what I have achieved with these pieces of code:
Send:
public void SendPacket(Packet pkt)
{
NetworkStream Stream = Socket.GetStream();
Byte[] Buffer = pkt.Serialize();
Byte[] Size = new Byte[8];
long len = Buffer.Length;
using (MemoryStream mem = new MemoryStream(Size))
{
using (BinaryWriter BW = new BinaryWriter(mem))
{
BW.Write(len);
}
}
Byte[] Bytes = new Byte[8 + Buffer.Length];
Array.Copy(Size, Bytes, Size.Length);
Array.Copy(Buffer, 0, Bytes, 8, Buffer.Length);
Stream.Write(Bytes, 0, Bytes.Length);
}
Receive:
private void StreamUpdater()
{
while(true){
NetworkStream Stream = Socket.GetStream();
if (Stream.DataAvailable)
{
Byte[] Bytes = new Byte[0];
Byte[] buffer = new Byte[8];
Stream.Read(buffer, 0, 8);
long i;
using (MemoryStream mem = new MemoryStream(buffer))
{
using (BinaryReader BR = new BinaryReader(mem))
{
i = BR.ReadInt64();
}
}
long BiM = 0;
if(DataArrived != null)
DataArrived.Invoke(this, new TcpEventArgs(new Packet(Packet.Type.Null, null)));
Array.Clear(buffer, 0, 8);
Array.Resize(ref buffer, 1024);
while (BiM < i)
{
int bytesRead;
int BytesToRead = 1024;
if (BytesToRead > (i - BiM)) BytesToRead = (int)(i - BiM);
if ((bytesRead = Stream.Read(buffer, 0, BytesToRead)) > 0)
{
Array.Resize(ref Bytes, Bytes.Length + bytesRead);
Array.Copy(buffer, 0, Bytes, BiM, bytesRead);
BiM += bytesRead;
double percent = BiM / i;
percent = percent * 100;
PercentReceived = (int)percent;
}
}
Packet pkt = SerialMediator.Deserialize(Bytes);
if (DataAvailable != null)
{
DataAvailable.Invoke(this, new TcpEventArgs(pkt));
}
}
}
}
Ignore the last four lines of code. The Packet and SerialMediator classes are classes I made to wrap up data being sent between the server and client, and the DataArrived is a custom event I created. The TcpEventArgs is a custom EventArgs class I created.