0

私は初めてパケットとバイトを処理しようと試みましたが、これまでさまざまな手法を試しましたが、パケットの長さを正しく取得できませんでした。

コード:

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

これまでのところ、コンソールにデータ長を書き込む試みはすべて失敗に終わりました。エンディアンを検証し、実際にバイトを書き出して、正しいデータを処理していることを確認しました。

バイト例:

00 00 00 24 -> UINT32 は 36 バイトですが、3808493568 のような整数の配列を取得しています

どうすればこれを修正できますか?

4

1 に答える 1

1

私はハンスに同意します。エンディアンはあなたの問題です。また、ストリームを使用するよりも簡単に、配列でBitConverterクラスを使用することをお勧めします。clientPacket

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

あなたのコードでは、これはうまくいくと思います(テストされていません):

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)
于 2014-06-05T02:20:11.220 に答える