[Help] – WebSocket Masked Frame

I am trying to implement a WebSocket using c# for my server and javascript for the client (in a web page).

I recieve this error from the Chrome Console when the server tries to echo back the message:

 WebSocket connection to 'ws://' failed: A server must not mask any frames that it sends to the client.ws.onopen @ index.html:13 

The following is my source code:


 using System; using System.Net.Sockets; using System.Net; using System.Security.Cryptography; using System.Threading; class Program { static Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP); static private string guid = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; static void Main(string[] args) { serverSocket.Bind(new IPEndPoint(IPAddress.Any, 8080)); serverSocket.Listen(128); serverSocket.BeginAccept(null, 0, OnAccept, null); Console.Read(); } private static void OnAccept(IAsyncResult result) { byte[] buffer = new byte[1024]; try { Socket client = null; string headerResponse = ""; if(serverSocket != null && serverSocket.IsBound) { client = serverSocket.EndAccept(result); var i = client.Receive(buffer); headerResponse = (System.Text.Encoding.UTF8.GetString(buffer)).Substring(0, i); Console.WriteLine(headerResponse); } if (client != null) { /* Handshaking and managing ClientSocket */ var key = headerResponse.Replace("ey:", "`") .Split('`')[1] .Replace("\r", "").Split('\n')[0] .Trim(); var test1 = AcceptKey(ref key); var newLine = "\r\n"; var response = "HTTP/1.1 101 Switching Protocols" + newLine + "Upgrade: websocket" + newLine + "Connection: Upgrade" + newLine + "Sec-WebSocket-Accept: " + test1 + newLine + newLine //+ "Sec-WebSocket-Protocol: chat, superchat" + newLine //+ "Sec-WebSocket-Version: 13" + newLine ; client.Send(System.Text.Encoding.UTF8.GetBytes(response)); var i = client.Receive(buffer); // wait for client to send a message // once the message is received decode it in different formats Console.WriteLine(Convert.ToBase64String(buffer).Substring(0, i)); Console.WriteLine("\n\nPress enter to send data to client"); var subA = SubArray<byte>(buffer, 0, i); client.Send(subA); Console.WriteLine("Sent Message"); Thread.Sleep(10000);//wait for message to be sent } } catch(SocketException exception) { throw exception; } finally { if(serverSocket != null && serverSocket.IsBound) { serverSocket.BeginAccept(null, 0, OnAccept, null); } } } public static T[] SubArray<T>(T[] data, int index, int length) { T[] result = new T[length]; Array.Copy(data, index, result, 0, length); return result; } private static string AcceptKey(ref string key) { string longKey = key + guid; byte[] hashBytes = ComputeHash(longKey); return Convert.ToBase64String(hashBytes); } static SHA1 sha1 = SHA1CryptoServiceProvider.Create(); private static byte[] ComputeHash(string str) { return sha1.ComputeHash(System.Text.Encoding.ASCII.GetBytes(str)); } } 


 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ift.tt/nuHeHq; <html xmlns="http://ift.tt/z3Mkxh; <head> <script type="text/javascript"> function connect(){ alert("everything looks good... sorta") var ws = new WebSocket("http://ws10.0.1.69:8080"); ws.onopen = function(){ alert("About to send data"); ws.send("Hello World"); alert("Message sent!"); }; ws.onmessage = function(evt){ alert("About to receive data"); var message = evt.data; alert("Message received: " + message); }; ws.onclose = function(){ alert("Connection is closed"); } } </script> </head> <body style="font-size:xx-large"> <div> <a href="#" onclick="connect()">Click here to start</a> </div> </body> </html> 

Here is the console output:

 GET / HTTP/1.1 Host: Connection: Upgrade Pragma: no-cache Cache-Control: no-cache Upgrade: websocket Origin: file:// Sec-WebSocket-Version: 13 DNT: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36 Accept-Encoding: gzip, deflate, sdch Accept-Language: en-US,en;q=0.8 Sec-WebSocket-Key: mC7pXfoUfRZkA0mL0KTP0g== Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits gYtc8uKPFJeO4zPSt Sent Message ^c 

How can I fix this error? Sorry for the massive source dump, but congrats, you made it to the end!

Thanks for reading


by Senior-Jesticle via /r/csharp

Leave a Reply