Question

Since NetworkStream.Length was never implemented how else could I see how much data is already available on the networkstream.

Note I can't simply just Read() while DataAvailable=True I use a thread where it keeps running a function while DataAvailable is set, then this function should cut that batch off.

Here is my broken function just to show what I'm trying to achieve.

All my packets are sent with [2 Bytes Size][1 Byte Type][Optional Payload]

Public Function readPacket() As PacketReader
    'This cuts the stream of bytes into readable packets.
    Try
        If serverStream.DataAvailable Then
            If packetSize = 0 Then
                packetSize = serverReader.ReadUInt16()
                packetType = serverReader.ReadByte()
            End If

            If packetSize = 0 Then
                Return New PacketReader(packetType, New MemoryStream())
            End If

            If serverStream.Length >= packetSize Then
                Dim packet() As Byte = serverReader.ReadBytes(packetSize)
                Dim stream As Stream = New MemoryStream(packet)
                Dim tmpPacketSize As UShort = packetSize
                packetSize = 0
                Return New PacketReader(packetType, tmpPacketSize, stream)
            End If
        End If
    Catch e As Exception
        formLobby.logMsg("Exception: " + e.ToString)
    End Try
    Return Nothing
End Function
Was it helpful?

Solution

Fixed it turns out. You cannot use the NetworkStream.Length function. You need to use TcpClient.Length instead.

which contains the NetworkStream

aka

TcpClient.GetStream() returns NetworkStream

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top