Click here to Skip to main content
15,884,077 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I want to avoid trimming of data send from a client to server interface via web socket. I am doing the connectivity and message sending with the client and server interface as separate Windows Application projects. I am able to connect with the server from client while sending smaller data. While sending larger volume of data, data is getting trimmed.

I increased the buffer size but still data is getting trimmed.

byte[] receiveBuffer = new byte[1024*4]


What I have tried:

Core Code as below:
Client lEVEL:
#region Send
       private async Task Send(ClientWebSocket webSocket)
       {
           try
           {
               NeSTCommonClass.WriteTextFile(LogPath + Logfilename, "Client:Sending to Server Process Begins", true);
               // while (webSocket.State == WebSocketState.Open)

               if (webSocket.State == WebSocketState.Open)
                   {
                   //Console.WriteLine("Write some to send over to server..");
                   //string stringtoSend = Console.ReadLine();
                   string stringtoSend = txtOutgoing.Text;

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

                   await webSocket.SendAsync(new ArraySegment<byte>(buffer), WebSocketMessageType.Binary, false, CancellationToken.None);
                   // Console.WriteLine("Sent:     " + stringtoSend);


                   await Task.Delay(1000);
               }
           }
           catch (Exception ex)
           {
               NeSTCommonClass.WriteTextFile(LogPath + Logfilename, "Client:Sending to Server Process Failed: " + ex.Message.ToString() + "", true);
           }
           NeSTCommonClass.WriteTextFile(LogPath + Logfilename, "Client:Sending to Server Process Ends", true);
       }
       #endregion


Server Level:
processing received data from Client is as below:
Processing the client level request from server as below;
byte[] receiveBuffer = new byte[1024 * 10];

               WebSocketReceiveResult receiveResult = await webSocket.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), CancellationToken.None);
                   var ServerstrReceived = System.Text.Encoding.Default.GetString(receiveBuffer);

                   txtIncoming.Text += Environment.NewLine + "Client:" + ServerstrReceived.ToString();

                   //Added to send to Client from Server

                   //string stringtoSend = txtOutgoing.Text;

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

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

                   if (receiveResult.MessageType == WebSocketMessageType.Close)
                       await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);
Posted
Updated 5-Jul-18 0:12am
v3
Comments
Richard MacCutchan 5-Jul-18 4:26am    
Your client receiver must keep receiving until it gets a message from the server that there is no more data. The chances are that you are receiving a partial message and then ignoring some following data. But that is just a guess as you have not shown your client code.
ranio 5-Jul-18 6:12am    
Please see the entire core code to send and receive using web socket as shown below: Updated in the Post too . Awaiting a solution asap.

Core Code as below:
Client lEVEL:
 #region Send        private async Task Send(ClientWebSocket webSocket)        {            try            {                NeSTCommonClass.WriteTextFile(LogPath + Logfilename, "Client:Sending to Server Process Begins", true);                // while (webSocket.State == WebSocketState.Open)                if (webSocket.State == WebSocketState.Open)                    {                    //Console.WriteLine("Write some to send over to server..");                    //string stringtoSend = Console.ReadLine();                    string stringtoSend = txtOutgoing.Text;                    byte[] buffer = encoding.GetBytes(stringtoSend);                    await webSocket.SendAsync(new ArraySegment<byte>(buffer), WebSocketMessageType.Binary, false, CancellationToken.None);                    // Console.WriteLine("Sent:     " + stringtoSend);                                      await Task.Delay(1000);                }            }            catch (Exception ex)            {                NeSTCommonClass.WriteTextFile(LogPath + Logfilename, "Client:Sending to Server Process Failed: " + ex.Message.ToString() + "", true);            }            NeSTCommonClass.WriteTextFile(LogPath + Logfilename, "Client:Sending to Server Process Ends", true);        }        #endregion


Server Level:
processing received data from Client is as below:
Processing the client level request from server as below;
 byte[] receiveBuffer = new byte[1024 * 10];                WebSocketReceiveResult receiveResult = await webSocket.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), CancellationToken.None);                    var ServerstrReceived = System.Text.Encoding.Default.GetString(receiveBuffer);                    txtIncoming.Text += Environment.NewLine + "Client:" + ServerstrReceived.ToString();                    //Added to send to Client from Server                    //string stringtoSend = txtOutgoing.Text;                    //byte[] buffer = encoding.GetBytes(stringtoSend);                    //await webSocket.SendAsync(new ArraySegment<byte>(buffer), WebSocketMessageType.Binary, false, CancellationToken.None);                    //Added to send to Client                    if (receiveResult.MessageType == WebSocketMessageType.Close)                        await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);                  
ranio 5-Jul-18 9:01am    
Another option to resolve the same was done as below:
but the websocketresult.EndofMessage() is coming as false. I passed simple string "test" and hence getting looped with in it repeatedly.
ArraySegment<byte> buffer = new ArraySegment<byte>(new Byte[8192]);

WebSocketReceiveResult result = null;

using (var ms = new MemoryStream())
{
do
{
result = await webSocket.ReceiveAsync(buffer, CancellationToken.None);



ms.Write(buffer.Array, buffer.Offset, result.Count);
}

while (!result.EndOfMessage) ;

ms.Seek(0, SeekOrigin.Begin);

if (result.MessageType == WebSocketMessageType.Text)
{
using (var reader = new StreamReader(ms, Encoding.UTF8))
{
// reader.ReadToEnd();
txtOutgoing.Text = reader.ReadToEnd();
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900