简体   繁体   中英

Data not readable at server in c# winform

I have created a server using SimpleTcpServer and I am able to receive data from a client machine the client machine code is written in c/c++ and data is sent from client machine in Qbytearray format, the data that I receive is not readable I have attached the pic kindly refer the pic for know the kind of data I receive those 3 box are the only data I am able to receive and those 3 boxes contain more than 500 byte array which is not readable enter image description here

Below is the code for received the data from client machine.

private void Events_DataReceived(object sender, DataReceivedEventArgs e)
        {
            //This method is where data is received
            this.Invoke((MethodInvoker)delegate
            {
                /*string str = Encoding.UTF8.GetString(e.Data);
                string s3 = Convert.ToBase64String(e.Data);
                string jsonStr = Encoding.UTF8.GetString(e.Data);
                Dictionary<String, Object> values = JsonConvert.DeserializeObject<Dictionary<String, Object>>(jsonStr);
                byte[] bytes = Encoding.Default.GetBytes(str);
                str = Encoding.UTF8.GetString(e.Data);       
                byte[] myByteArray = new byte[str.Length];
                for (int ix = 0; ix < str.Length; ++ix)      //These are all possible ways I tried
                {
                    char ch = str[ix];
                    myByteArray[ix] = (byte)ch;
                }
                str = Encoding.UTF8.GetString(myByteArray, 0, str.Length);
                txtInfo.Text += $"{e.IpPort}: {values}{Environment.NewLine}";*/ //Hidden Part


               //Currently used code(I receive bytes in e.Data but are not readable)
         txtInfo.Text += $"{e.IpPort}: {Encoding.UTF8.GetString(e.Data)}{Environment.NewLine}";
            });
        }

Please note I am able to receive normal string data like chat app in the server but the client machine code was not written by me on which I am working and the data is not compressed the data which client is sending is in Qbytearray format

Is there any way through which I can make data readable or find how or which format I am receiving the data GetType just gives me output as UTF8 and GetString provides me data as above

Any help will be appreciated..:'(

Try this:

            string input = "abc\0defgh\0ijk\0lmnopqrst\0\0";
            byte[] bytes = Encoding.UTF8.GetBytes(input);
            List<string> outputs = null;

            int ptr = 0;
            while (true)
            {
                int endByte = Array.IndexOf(bytes, (byte)0, ptr);
                if ((endByte == -1) || (ptr == endByte)) break;
                
                if (outputs == null) outputs = new List<string>();

                string output = Encoding.UTF8.GetString(bytes, ptr, endByte - ptr);
                outputs.Add(output);

                ptr = endByte + 1;
            }

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