2

I'm able to receive data from the client over the WebSocket but I don't understand how to send data back from the server to the client. How do I send data back to the WebSocket, I'm now getting "?Masked frame from server" as a response from the Websocket when I try to send data to it. How do i encode the data to be sent to the WebSocket?

Nova1545
  • 81
  • 1
  • 8
  • The question has the only errors it output and frankly shouldn't have been closed, I even answer the question below – Nova1545 May 17 '20 at 15:57

2 Answers2

1

I needed to encode the data I was sending to the client, the code below was what fixed it (Used from https://stackoverflow.com/a/8125509/10809344) but change to be in c#

byte[] raw = Encoding.UTF8.GetBytes("Message Recevied");

byte[] frame = new byte[10 + raw.Length];
int indexStartData = -1;

frame[0] = 129;

if(raw.Length <= 125)
{
    frame[1] = (byte)raw.Length;
    indexStartData = 2;
}
else if(raw.Length >= 126 && raw.Length <= 65535)
{
    frame[1] = 126;
    frame[2] = (byte)((raw.Length >> 8) & 255);
    frame[3] = (byte)((raw.Length) & 255);

    indexStartData = 4;
}
else
{
    frame[1] = 127;
    frame[2] = (byte)((raw.Length >> 56) & 255);
    frame[3] = (byte)((raw.Length >> 48) & 255);
    frame[4] = (byte)((raw.Length >> 40) & 255);
    frame[5] = (byte)((raw.Length >> 32) & 255);
    frame[6] = (byte)((raw.Length >> 24) & 255);
    frame[7] = (byte)((raw.Length >> 16) & 255);
    frame[8] = (byte)((raw.Length >> 8) & 255);
    frame[9] = (byte)((raw.Length) & 255);

    indexStartData = 10;
}

for (int i = 0; i < raw.Length; i++)
{
    frame[i + indexStartData] = raw[i];
}
stream.Write(frame, 0, frame.Length);
Nova1545
  • 81
  • 1
  • 8
  • Hello man .. This encoding method same as main .. and i received the message on client javascript, but the second message i could not get it, can you see my Q ? i will post the url. thank you. – obeid salem Apr 19 '22 at 02:30
  • https://stackoverflow.com/questions/71918536/c-sharp-websocket-after-sending-the-second-message-i-got-a-server-must-not – obeid salem Apr 19 '22 at 02:31
0

According to the documentation about the TcpListener it's only possible to send responses from the listener after receiving data from the client. You can alter the example from the documentation to achieve your specific goal. Just make it a bit easier for you I've made a simplified version of that sample

Example

Int32 port = 13000;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");

TcpListener server = new TcpListener(localAddr, port);

// Start listening for client requests.
server.Start();

// Buffer for reading data.
Byte[] bytes = new Byte[256];

// Entering a Listening loop.
while (true)
{
    // Perform a blocking call to accept requests.
    // 
    TcpClient client = server.AcceptTcpClient();

    // A stream object for reading and writing.
    NetworkStream stream = client.GetStream();

    int i;

    // Loop to receive all the data sent by the client.
    while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
    {
        // Process the received data...

        // Send a response back to the Client:
        //      1. Turn a message or a set of data into a byte[] (array); 
        //      2. stream.Write(byte[] buffer, int offset, int size);
    }

    // Shutdown and end connection.
    client.Close();
}

Sources:

  1. https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.tcplistener?view=netcore-3.1;
  2. How can we send the data using TcpListener in c#?
Iliass Nassibane
  • 651
  • 7
  • 15
  • Thanks for the response but this doesn't help me in my issue, I updated the question with some newer information I have found – Nova1545 May 13 '20 at 15:39
  • @AidenWilkins That additional information makes a huge difference in the actual question that's being asked. I'll see what I can do for ya. – Iliass Nassibane May 13 '20 at 15:52
  • Hello man ,, can u help me with my Q ? https://stackoverflow.com/questions/71918536/c-sharp-websocket-after-sending-the-second-message-i-got-a-server-must-not – obeid salem Apr 19 '22 at 02:32