简体   繁体   中英

Decoding binary received over tcp socket in c#?

I have several linux routers setup that sniff nearby traffic. They have sockets open in order for an application to be connected over tcp, through the sockets data is streamed in binary.

I've built a simple application to check if data is being received, and it is, however, it's giberish. Seems like binary isn't decoded properly. Does anyone have any clue?

        try
        {
            TcpClient ourMagicClient = new TcpClient();
            ourMagicClient.Connect("192.168.0.101", Port);
            NetworkStream ourStream = ourMagicClient.GetStream();
            byte[] data = new byte[ourMagicClient.ReceiveBufferSize];
            int offset = 0;

            for (int i = 0; i < 20; i++)
            {
                int bytesRead = ourStream.Read(data, 0, System.Convert.ToInt32(ourMagicClient.ReceiveBufferSize));
                output += Encoding.ASCII.GetString(data, 0, bytesRead);

                Console.WriteLine(output);
                //Console.WriteLine(encoding.); 
                Console.WriteLine("\n");
                offset = bytesRead;
            }

            System.IO.File.WriteAllText(@"C:\Users\qweik_000\Desktop\WriteText.txt", output, Encoding.UTF8);

            Console.WriteLine("Press enter to close...");
            Console.ReadLine();
        }
        catch (Exception e)
        {
            Console.WriteLine("Error : " + e.Message);
            Console.WriteLine("Error : " + e.StackTrace);
            Console.ReadLine();
        }

What I see in my console app upon running it :

在此处输入图片说明

您可能会寻找有效ASCII范围(即0x20到0x7f)中的字节字符串(连续的一些字节),并提取并显示此“可读文本”作为开始...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM