Hallo,
ich habe hier ein Websocket-Script welches ich mir aus ein paar Foren ein bisschen zusammen gewurschtelt habe und habe jetzt allerdings ein Problem, das Programm beendet sich einfach Sang- und Klanglos in der Zeile 99.
Sonst klappt alles perfekt, Daten werden vom Client und zurück gesendet.
Weiß zufällig jemand warum das Programm abbricht?
ich habe hier ein Websocket-Script welches ich mir aus ein paar Foren ein bisschen zusammen gewurschtelt habe und habe jetzt allerdings ein Problem, das Programm beendet sich einfach Sang- und Klanglos in der Zeile 99.
Sonst klappt alles perfekt, Daten werden vom Client und zurück gesendet.
Weiß zufällig jemand warum das Programm abbricht?
C#-Quellcode
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Security.Cryptography;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace WebSockets_Test
- {
- 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);
- // write received data to the console
- Console.WriteLine(headerResponse);
- }
- if (client != null)
- {
- /* Handshaking and managing ClientSocket */
- var key = headerResponse.Replace("ey:", "`")
- .Split('`')[1] // dGhlIHNhbXBsZSBub25jZQ== \r\n .......
- .Replace("\r", "").Split('\n')[0] // dGhlIHNhbXBsZSBub25jZQ==
- .Trim();
- // key should now equal dGhlIHNhbXBsZSBub25jZQ==
- 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
- ;
- // which one should I use? none of them fires the onopen method
- 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));
- try
- {
- if (serverSocket != null && serverSocket.IsBound)
- {
- var Respone = EncodeMessageToSend("Test");
- client.Send(Respone);
- }
- }
- catch (Exception ex)
- {
- Console.Write(ex);
- }
- Thread.Sleep(5000);//wait for message to be send
- }
- }
- catch (SocketException exception)
- {
- throw exception;
- }
- finally
- {
- if (serverSocket != null && serverSocket.IsBound)
- {
- try
- {
- serverSocket.BeginAccept(null, 0, OnAccept, null);
- }
- catch (SocketException exception)
- {
- throw exception;
- }
- }
- }
- }
- private static Byte[] EncodeMessageToSend(String message)
- {
- Byte[] response;
- Byte[] bytesRaw = Encoding.UTF8.GetBytes(message);
- Byte[] frame = new Byte[10];
- Int32 indexStartRawData = -1;
- Int32 length = bytesRaw.Length;
- frame[0] = (Byte)129;
- if (length <= 125)
- {
- frame[1] = (Byte)length;
- indexStartRawData = 2;
- }
- else if (length >= 126 && length <= 65535)
- {
- frame[1] = (Byte)126;
- frame[2] = (Byte)((length >> 8) & 255);
- frame[3] = (Byte)(length & 255);
- indexStartRawData = 4;
- }
- else
- {
- frame[1] = (Byte)127;
- frame[2] = (Byte)((length >> 56) & 255);
- frame[3] = (Byte)((length >> 48) & 255);
- frame[4] = (Byte)((length >> 40) & 255);
- frame[5] = (Byte)((length >> 32) & 255);
- frame[6] = (Byte)((length >> 24) & 255);
- frame[7] = (Byte)((length >> 16) & 255);
- frame[8] = (Byte)((length >> 8) & 255);
- frame[9] = (Byte)(length & 255);
- indexStartRawData = 10;
- }
- response = new Byte[indexStartRawData + length];
- Int32 i, reponseIdx = 0;
- //Add the frame bytes to the reponse
- for (i = 0; i < indexStartRawData; i++)
- {
- response[reponseIdx] = frame[i];
- reponseIdx++;
- }
- //Add the data bytes to the response
- for (i = 0; i < length; i++)
- {
- response[reponseIdx] = bytesRaw[i];
- reponseIdx++;
- }
- return response;
- }
- 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));
- }
- }
- }