我正在制作一个简单的 TCP 接收器来处理某些设备发送的数据。代码非常简单,我也能够接收数据。以下是我的代码
TcpListener server = null;
try
{
// Set the TcpListener on port.
Int32 port = int.Parse(configuration["PortToListen"]);
IPAddress localAddr = IPAddress.Parse(configuration["IPToListen"]);
// TcpListener server = new TcpListener(port);
server = new TcpListener(localAddr, port);
// Start listening for client requests.
server.Start();
// Enter the listening loop.
while (true)
{
Log.Information($"Waiting for a connection @ {configuration["IPToListen"]} : {configuration["PortToListen"]}... ");
// Perform a blocking call to accept requests.
// You could also use server.AcceptSocket() here.
TcpClient client = server.AcceptTcpClient();
Log.Information("Client Connection Established!");
// Uses the GetStream public method to return the NetworkStream.
NetworkStream netStream = client.GetStream();
if (netStream.CanRead)
{
// Reads NetworkStream into a byte buffer.
byte[] bytes = new byte[655351];
// Read can return anything from 0 to numBytesToRead.
// This method blocks until at least one byte is read.
netStream.Read(bytes, 0, (int)65535);
// Returns the data received from the host to the console.
string returndata = Encoding.ASCII.GetString(bytes);
returndata = returndata.Trim();
Log.Information("--------Following is the data received from site---------\r\n\r\n");
Log.Information(returndata);
}
netStream.Close();
// Shutdown and end connection
client.Close();
}
}
catch (Exception e)
{
Log.Error(e, "Unable to listen to port - " + e.Message);
throw;
}
finally
{
// Stop listening for new clients.
server.Stop();
}
但是我收到的数据是八位字节流,如下所示
Host: 127.0.0.1:8081
Content-Type: application/octet-stream
X-Forwarded-For: 127.0.0.1
X-Forwarded-Host: 127.0.0.1:8081
X-Forwarded-Server: 127.0.0.1
Connection: Keep-Alive
Content-Length: 296
0?$public??? 0?0+ C2E0
下面的截图
我相信发送的数据是 SNMP 格式/八位字节流。有什么办法可以解码吗?我尝试搜索但没有运气