0

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

RABI
  • 692
  • 2
  • 15
  • 1
    https://github.com/lextudio/sharpsnmplib/blob/master/SharpSnmpLib/Messaging/MessageFactory.cs#L40 If the bytes are in SNMP format, you can parse them using #SNMP Library. – Lex Li Jul 15 '21 at 13:15
  • @LexLi is there option to convert string to PDU using this #SNMP Library? Any sample link available? – RABI Jul 29 '21 at 11:24
  • What do you mean by "string to PDU"? SNMP protocol never defines something like that. – Lex Li Jul 29 '21 at 12:57
  • @LexLi the client side app is developed in c++ and it is converting PDU to string and sending it to server as http request in body. So in the server side, i have to convert it back to PDU. In the screenshot i shared in question, this is the string version of data i am receiving in the server, which include the header information as well. There is a server side application developed in c++ as well which is doing the same thing, converting this string to PDUs in 2 steps, convert string to snmp message, then convert to pdu. But i want to implement this in c# – RABI Jul 29 '21 at 13:36
  • 1
    You can only convert bytes from/to PDU, so you need to determine how bytes can be converted to strings without losing anything. If algorithms like Base64 are used, the C# code should be simple to write. – Lex Li Jul 29 '21 at 16:16

0 Answers0