0
Hi,
Here is the code to create a websocket server in c#, it opens a TCP server on the port 8081.
/// <summary> /// Websocket server /// </summary> public static void Exercice19() { Byte[] bytes = new Byte[256]; int port = 8081; string data; TcpListener server = new TcpListener(IPAddress.Loopback, port); server.Start(); //Listening connections while (true) { Console.WriteLine("Waiting a new connection"); //Bloquer le block tant qu'un client ne s'est pas connectés TcpClient client = server.AcceptTcpClient(); Console.WriteLine("Client connected");
//Make write and read operations to the network stream NetworkStream stream = client.GetStream(); int i; data = string.Empty; //Read received values while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) { data = System.Text.Encoding.ASCII.GetString(bytes, 0, i); Console.WriteLine(data); } stream.Close(); } }