Parse Octet Stream / SNMP using C#

361 views Asked by At

I am making a simple TCP Receiver to handle data send by some device. The code is pretty straight forward and i am able to receive the data as well. Following is my code

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();
}

But the data i receive is octet stream as follows

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

Screenshot below

enter image description here

I believe the data is sent is SNMP format / octet-stream. Is there any way to decode this? I tried searching but no luck

0

There are 0 answers