0

由于NetworkStream.Length从未实施过,我怎么能看到网络流上已经有多少数据可用。

注意我不能只是Read()DataAvailable=True我使用一个线程时,它在设置时继续运行一个函数DataAvailable,那么这个函数应该切断那个批处理。

这是我的破碎功能,只是为了展示我想要实现的目标。

我所有的数据包都是用[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
4

1 回答 1

0

事实证明,已修复。您不能使用该NetworkStream.Length功能。你需要TcpClient.Length改用。

其中包含NetworkStream

又名

TcpClient.GetStream()返回NetworkStream

于 2013-07-04T16:27:46.987 回答