Guest User

Untitled

a guest
Feb 13th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. static TcpListener server = new TcpListener(IPAddress.Any, 8080);
  2. public static void Main()
  3. {
  4.  
  5. server.Start();
  6. serverstart();
  7. }
  8.  
  9. public static void serverstart()
  10. {
  11. TcpClient client = server.AcceptTcpClient();
  12.  
  13. Console.WriteLine("A client connected.");
  14.  
  15. NetworkStream stream = client.GetStream();
  16.  
  17. //enter to an infinite cycle to be able to handle every change in stream
  18.  
  19. while (true)
  20. {
  21. while (!stream.DataAvailable) ;
  22.  
  23. Byte[] bytes = new Byte[client.Available];
  24.  
  25. stream.Read(bytes, 0, bytes.Length);
  26.  
  27. String data = Encoding.UTF8.GetString(bytes);
  28.  
  29. if (new Regex("^GET").IsMatch(data))
  30. {
  31. Byte[] response = Encoding.UTF8.GetBytes("HTTP/1.1 101 Switching Protocols" + Environment.NewLine
  32. + "Connection: Upgrade" + Environment.NewLine
  33. + "Upgrade: websocket" + Environment.NewLine
  34. + "Sec-WebSocket-Accept: " + Convert.ToBase64String(
  35. SHA1.Create().ComputeHash(
  36. Encoding.UTF8.GetBytes(
  37. new Regex("Sec-WebSocket-Key: (.*)").Match(data).Groups[1].Value.Trim() + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
  38. )
  39. )
  40. ) + Environment.NewLine
  41. + Environment.NewLine);
  42. Console.WriteLine(data);
  43. stream.Write(response, 0, response.Length);
  44. }
  45. else
  46. {
  47.  
  48.  
  49. Console.WriteLine(data);
  50. }
  51.  
  52.  
  53. }
  54. }
  55. }
Add Comment
Please, Sign In to add comment