title: C# HTML5 Websocket Server
body: <p>I'm trying to create a C# Websocket server but I just don't seem to get it working.
  I now have a server that accepts the TCPClient, receives the HTTP request from the client and that tries to send back a HTTP response so the HTML5 WebSocket handshake can be completed.</p>
  <p>I believe there is something wrong with my handshake that the server sends to the client.
  I read the draft (<a href="https://datatracker.ietf.org/doc/html/draft-hixie-thewebsocketprotocol-76#section-1.3" rel="nofollow noreferrer">Websocket 76 draft</a>) which states that at the end of the handshake a response has to be given for the two keys that are given. This response gets calculated by the server.</p>
  <p>This is my code:</p>
  <pre><code>static void Main(string[] args)
      {
          int port = 8181;
          IPAddress localAddr = IPAddress.Loopback;
  
          TcpListener server = new TcpListener(localAddr, port);
          server.Start();
  
          // Buffer for reading data
          Byte[] receivedBytes = new Byte[256];
          String data = null;
  
          // Enter the listening loop.
          while (true)
          {
              Console.WriteLine(&quot;Waiting for a connection...&quot;);
  
              // Perform a blocking call to accept requests.
              // You could also user server.AcceptSocket() here.
              TcpClient client = server.AcceptTcpClient();
              Console.WriteLine(&quot;Connected!\n&quot;);
  
              data = null;
  
              // Get 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(receivedBytes, 0, receivedBytes.Length)) != 0)
              {
                  // Translate data bytes to a ASCII string.
                  data = System.Text.Encoding.UTF8.GetString(receivedBytes, 0, i);
  
                  Console.WriteLine(&quot;Received:&quot;);
                  Console.WriteLine(data);
                  Byte[] response_token = hashResponse(data);
  
  
                  string handshake = &quot;HTTP/1.1 101 WebSocket Protocol Handshake\r\n&quot;
                      + &quot;Upgrade: WebSocket\r\n&quot; + &quot;Connection: Upgrade\r\n&quot;
                      + &quot;Sec-WebSocket-Origin: http://localhost\r\n&quot;
                      + &quot;Sec-WebSocket-Location: ws://localhost:8181/websession\r\n&quot;
                      + &quot;\r\n&quot;;
  
                  Byte[] writtenBytes = Encoding.UTF8.GetBytes(handshake);
  
                  stream.Write(writtenBytes, 0, writtenBytes.Length);
                  stream.Write(response_token, 0, response_token.Length);
  
                  Console.WriteLine(&quot;Send:&quot;);
                  Console.WriteLine(handshake);
  
                  string strHash = Encoding.UTF8.GetString(response_token);
                  Console.WriteLine(strHash);
              }                
          }
      }
  
      static Byte[] hashResponse(string receivedData)
      {
          string strDel = &quot;\r\n&quot;;
          char[] delimeter = strDel.ToCharArray();
  
          string Key1 = null;
          string Key2 = null;
          string hash = null;
          MD5 md5 = MD5.Create();
  
          string[] lines = receivedData.Split(delimeter);
          Key1 = lines[10].Substring(20);
          Key2 = lines[12].Substring(20);
          hash = lines[16];
  
          Int64 numbersKey1 = Convert.ToInt64(string.Join(null, Regex.Split(Key1, &quot;[^\\d]&quot;)));
          Int64 numbersKey2 = Convert.ToInt64(string.Join(null, Regex.Split(Key2, &quot;[^\\d]&quot;)));
  
          Int64 numberSpaces1 = countSpaces(Key1);
          Int64 numberSpaces2 = countSpaces(Key2);
  
          int dividedKey1 = (int) (numbersKey1 / numberSpaces1);
          int dividedKey2 = (int) (numbersKey2 / numberSpaces2);
  
          Byte[] encodedKey1 = Encoding.UTF8.GetBytes(dividedKey1.ToString());
          Byte[] encodedKey2 = Encoding.UTF8.GetBytes(dividedKey2.ToString());
          Byte[] encodedHash = Encoding.UTF8.GetBytes(hash);
  
          Byte[] combined = Encoding.UTF8.GetBytes(dividedKey1.ToString() + dividedKey2.ToString() + hash);
  
          Byte[] responseHash = md5.ComputeHash(combined); 
          return responseHash;
      }
  
      static int countSpaces(string key)
      {
          int counter = 0;
          char[] charArray = key.ToCharArray();
  
          foreach (char c in charArray)
          {
              if (c.Equals(' '))
                  counter++;
          }
  
          return counter;
      }
  </code></pre>
  <p>The HTML page (which is named Test.html) I'm using for testing is hosted by an apache webserver which runs on my computer I access it by browsing (in Chrome) to http://localhost/Test.html</p>
  <p>Does anyone have a clue what I'm doing wrong because I'm getting quite desperate.</p>
  <p>Thanks in advance</p>
  <p>Dennis</p>