Mattermost, Inc.

How to resolve the issue of \0 character while sending large data over web socket in c#?

I want to resolve the issue of \0 character while sending large data in chunks/fragments over web socket in c#. I want to send large data via web socket. I am splitting the large data into chunks based on the buffer size and looping until the end of message send from web socket becomes true.

Client Side:
#region Send
private async Task Send(ClientWebSocket webSocket)
{
try
{

            if (webSocket.State == WebSocketState.Open)
            {
                string stringtoSend = txtOutgoing.Text;

                byte[] buffer = encoding.GetBytes(stringtoSend);

                bool EOD = false;
                // byte[] text = Encoding.UTF8.GetBytes(longString);
                int len = 1000;
                for (int i = 0; i < buffer.Length;)
                {
                    byte[] chunk = null;
                    int j = 0;
                    if (buffer.Length-i < len)
                    {
                        EOD = true;
                        chunk = new byte[buffer.Length - i];
                    }
                    else
                    {
                        chunk=new byte[len];
                    }

                    //byte[] chunk = new byte[buffer.Length - i];
                    while (++j < chunk.Length && i < buffer.Length)
                    {
                        chunk[j] = buffer[i++];
                    }
                    await webSocket.SendAsync(new ArraySegment<byte>(chunk), WebSocketMessageType.Binary, EOD, CancellationToken.None);
                    //Convert(chunk); //do something with the chunk
                   
                }


               // await webSocket.SendAsync(new ArraySegment<byte>(buffer), WebSocketMessageType.Binary, false, CancellationToken.None);

               // await Task.Delay(1000);
            }
        }
        catch (Exception ex)
        {

        }
    }
    #endregion

SERVER

//while (webSocket.State == WebSocketState.Open)
if (webSocket.State == WebSocketState.Open)
{

                #region

                //var receiveBuffer = new byte[bufferSize];
                ////WebSocketReceiveResult receiveResult = await webSocket.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), CancellationToken.None);
                //WebSocketReceiveResult receiveResult = await webSocket.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), CancellationToken.None);

                #endregion


                var buffer = new ArraySegment<byte>(new byte[1000]);
                WebSocketReceiveResult receiveResult = null;
                var allBytes = new List<byte>();

                do
                {
                    receiveResult = await webSocket.ReceiveAsync(buffer, CancellationToken.None);
                    for (int i = 0; i < receiveResult.Count; i++)
                    {
                        allBytes.Add(buffer.Array[i]);
                    }
                }
                while (!receiveResult.EndOfMessage);

                // Optional step to convert to a string (UTF-8 encoding).
                var ServerstrReceived = Encoding.UTF8.GetString(allBytes.ToArray(), 0, allBytes.Count);

}

But in the output I am getting \0 appended in the beginning of each chunk but getting entire data
Output is as below:
\0

<?xml version=\"1.0\" encoding=\"utf-16\"?>

<Request
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance
xmlns:xsd=“http://www.w3.org/2001/XMLSchema”>


038
P049563445634674574325323674we5476956789057905694565745764256456
<password\0>6919301721649254400252100878956720265267481307739303873300957305
WS001
WSI835180322000000
jES+fXttCsMVnj+jcEE/VjlDp7uzVa85CuezsDMC3Q7HjujL1stguVGIIVg57ZSGHkSvF+0mt+JxkZpy8cZovAx1zaWW7rTYCjaRiwebWsQqf6WKZcgM+w6lYZ8e9aC6\09LGcYOb1/Y45BGyVJF3oSfgMBGj8a3d8FFqyCWcKaZk=


AP+Lf4cJdYTuYwASoN6KEVBZ5Xc5IndY9LHuQD/xEtLHbykGb0EoxlzoLlx3rGpVaBx3AoUBhBL5KqX/qmS/QxtM6zVL4s+xjqtL9idkKRArOvpFAEfKSUTuD2ojmNjGwO7mP+dwcGrAYuGcuFCMXY0lEXLhQ51h0cS0dw0zVulVRKAow/P82BP\0AKAlsSHHFjAB75JgWUwjOEK++F3rsp7mrMOYrWQWo2IQgQ//ph1WK42ywsGxvw+4DxQhC2FSu7yae5nS/upMtOMiCHLUH8mHNjt4RXHV747iPlFWMY0l8u3cz3QEteFEp2e4LzV8vt4bZal8QSqV3btFr2YQ0XQ==
d5v3oN4k8M7yP+YL4v/0j0l86AS3GLRCqaQ3xXriqnJBtdaRqOgnCPfXB0EE3nUvVTSsTw3NaL2de6t0b9twxfURRg\0js+te8b/ObMSlDZn2Fv4dg5H3qnlXdYvB1q7E+9zpvnEcMz23p7E91wp2zRAIpgFxH+l7VHPKfMfyUWx1b+QWBfKPRK2PpXH3Gm1rTSYjbKHv0Ol+Uuz7T7/uBUXDQ2T22/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jpc69g7gBnWQaZw3EQeTFV/UI6UlEepJuLjorv4eVKn/IxrJNrwZRQRJF5WFcpG+BG1mBzXvpmJKYLru0J8kmnLsFFIE5J4T+rh4jRir+RrWtc911nRrsYf5GrWcdUrqyRxg07ImXG4b7vgx1tNykA9yLfUNRS+vsixz01dOGtyO4wS0fJM9U48gxRRR4d90uhZGbaXztCpZwZAaP1BEfUyt5gRsQ8Oy4fmAhjKJ+JT/G+joUko18EKkgiLfPdi2lSBd3HzsvwN76Uv/xIPxqej0NpZN3PRd70nc=
</enc\0ryptedRequestData>


1 Like

Hi @alexander.junior! Thanks for reaching out.

Do you have links to any docs that you are following for this?