Click here to Skip to main content
15,886,791 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to send response messages from a server using web socket in c#. But am getting the exception as below:
Object reference issue.

From client to server i am able to send message using client web socket. But from server I am unable to send the message from client.

But  am getting the exception as below: 
Object reference issue. 


What I have tried:

Core Code of Client and Server sending messages
Client Level:
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);
              }


Server level:
WebSocketContext webSocketContext = null;
            try
            {
                // while (webSocket.State == WebSocketState.Open)
                WebSocket webSocket = webSocketContext.WebSocket;

                //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);

            }
            catch (Exception ex)
            {

            }
Posted
Updated 28-Jun-18 18:14pm

1 solution

sorry for the repost. Here the case is the server response is coming but only after sending the client message send second time around server response coming. Hence posted on the same.
 
Share this answer
 
Comments
Richard Deeming 29-Jun-18 10:16am    
In what way is this supposed to be a "solution" to your question?

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