質問
私は初めてパケットとバイトの取扱いで私の手を試してきました、そして私はさまざまなテクニックの後までに正しくパケットの長さを得ることができませんでした。
コード:
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
.
これまでのところ、コンソールを使用しているすべての試みはデータ長を故障しています。私はEndiannessを検証し、実際には私が正しいデータを処理していることを確認するためにバイトを書きました。
例バイト:
00 00 00 24 - > uint32は36バイトですが、3808493568のような整数の配列を取得しています。
どうすればいいですか?
解決
私はHANSに同意し、エンディアンはあなたの問題です。また、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)
. 所属していません StackOverflow