Skip to content

Creation of Websocket server in c#

Vivek Raut

Asked Aug 6, 2013, 11:08 PM EDT1 answer
Share
LinkedInX
Hi,
 
I want to create a websocket server in c# and HTML5 browser is my client. I want to send string text and a XML file from server to client via websocket. 
I got some code from internet but that code is not much useful for me. I got stuck into this so can anybody help me to resolve this as early as possible.
 
Configurations : 
1. Visual Studio 2010
2. Dot Net framework 4.0
3. Windows 7 and 32bit o.s
 

Thank you in advance.

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Hi, 

    Here is the code to create a websocket server in c#, it opens a TCP server on the port 8081.

    PowerShell
    /// <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();             }         }
    0