直接读取TCP数据包为十六进制

时间:2012-07-14 13:37:07

标签: vb.net tcp hex packets

我有System.Net.Sockets.TcpClient。我可以建立连接并准确地写入数据包。问题是想要按原样读取数据包(由嗅探器以十六进制形式捕获,如0F 03 56 56等。)

我尝试查看GetStream.write的示例,但我没有以这种方式阅读它们。我也尝试使用streamreader然后将数据包转换为十六进制,但我连接的东西发送搞砸的数据包,这些数据包无法转换或简单而不是字符串形式。

我希望我足够清楚。

Dim bytes(tcpClient.ReceiveBufferSize) As Byte 
' Read can return anything from 0 to numBytesToRead. 
' This method blocks until at least one byte is read. 
netStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize)) 
' Returns the data received from the host to the console. 
Dim returndata As String = Encoding.ASCII.GetString(bytes) 

1 个答案:

答案 0 :(得分:0)

找到解决方案!这是:

Dim Bytes() as Byte = New Byte(1024){}
Array.Resize(Bytes, TcpClient.Client.Receive(Bytes, SocketFlags.None))

For Each B As Byte In Bytes
    Debug.Print("Byte in HEX Format: " & B.ToString("X2"))
Next