Pergunta

Tenho estado a tentar a minha mão no processamento de pacotes e bytes, pela primeira vez, e eu não tenho sido capaz de obter o comprimento do pacote corretamente até agora depois de muitas técnicas diferentes.

Código:

Public Shared Sub Client(packet As Packet)
    Console.WriteLine( _ 
      "Client -> " & _
      packet.Timestamp.ToString("yyyy-MM-dd hh:mm:ss.fff") & _
      " length:" & Convert.ToString(packet.Length))

    'Define Byte Array
    Dim clientPacket As Byte() = packet.Buffer

    ' Open a Binary Reader
    Dim memStream As MemoryStream = New MemoryStream(clientPacket)
    Dim bReader As BinaryReader = New BinaryReader(memStream)

    ' Remove the Ethernet Header
    Dim ethBytes As Byte() = bReader.ReadBytes(14)

    ' Remove the IPv4 Header
    Dim IPv4Bytes As Byte() = bReader.ReadBytes(20)

    ' Remove the TCP Header
    Dim TCPBytes As Byte() = bReader.ReadBytes(20)

    ' Get the packet length
    If clientPacket.Length > 54 Then
        Dim len As UInt32 = bReader.ReadUInt32
        Console.WriteLine(len)
    End If
End Sub

Até agora, todas as minhas tentativas de ter o console escrever o comprimento de dados resultaram em fracasso.Eu validado a ordenação e, na verdade, escreveu bytes para verificar que eu estava lidando com os dados corretos.

Exemplo bytes:

00 00 00 24 -> UINT32 é de 36 bytes, mas estou recebendo uma matriz de números Inteiros como 3808493568

Como posso corrigir isso?

Foi útil?

Solução

Concordo com Hans, ordenação é o seu problema.Além disso, eu recomendo que você use o BitConverter classe a clientPacket matriz, mais fácil do que usando fluxos.

Dim len As UInt32
Dim arr() As Byte
arr = {0, 0, 0, 24}
len = BitConverter.ToUInt32(arr, 0)
Console.Write(len.ToString) 'returns 402653184

arr = {24, 0, 0, 0}
len = BitConverter.ToUInt32(arr, 0)
Console.Write(len.ToString) 'returns 24

Para o código, eu acho que isso pode funcionar (não testado):

If clientPacket.Length > 54 Then
  Dim lenBytes As Byte() = bReader.ReadBytes(4)
  Array.Reverse(lenBytes, 0, 4)
  Dim len As UInt32 = BitConverter.ToUInt32(lenBytes, 0)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top