websockets server trouble
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: websockets server trouble

  1. #1
    Join Date
    Dec 2009
    Posts
    22

    websockets server trouble

    I'm trying to implement a C# web socket server, but its giving me a few troubles.
    I'm running a webserver(ASP.NET) to host the page with the javascript and the web socket server is implemented as a C# console application.

    I'm able to detect the connection attempt from the client (chrome running the javascript) and also to retrieve the handshake from the client. But the client doesn't seem to accept the handshake I'm sending back (the `onopen` function on the web socket is never called).

    I've been reading the The Web Socket protocol and I can't see what I'm doing wrong.
    Heres a bit of the server code:
    Code:
        Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
        IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8181);
        listener.Bind(ep);
        listener.Listen(100);
        Console.WriteLine("Wainting for connection...");
        Socket socketForClient = listener.Accept();
        if (socketForClient.Connected)
        {
            Console.WriteLine("Client connected");
            NetworkStream networkStream = new NetworkStream(socketForClient);
            System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(networkStream);
            System.IO.StreamReader streamReader = new System.IO.StreamReader(networkStream);
        
            //read handshake from client:
            Console.WriteLine("HANDSHAKING...");
            char[] shake = new char[255];
            streamReader.Read(shake, 0, 255);
        
            string handshake =
               "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" +
               "Upgrade: WebSocket\r\n" +
               "Connection: Upgrade\r\n" +
               "WebSocket-Origin: http://localhost:8080\r\n" +
               "WebSocket-Location: ws://localhost:8181\r\n" +
               "\r\n";
        
            streamWriter.Write(handshake);
            streamWriter.Flush();
    I'm running to web server on port 8080 and the web socket server on port 8181, both on my localhost.

    I've tried sending the handshake in different encodings (ASCII, bytes and Hex) but this doesn't seem to make a difference. The connection is never fully established.
    The javascript looks like this:
    Code:
        var ws;
        var host = 'ws://localhost:8181';
        debug("Connecting to " + host + " ...");
        try {
         ws = new WebSocket(host);
        } catch (err) {
         debug(err, 'error');
        }
        ws.onopen = function () {
         debug("connected...", 'success');
        };
        ws.onclose = function () {
         debug("Socket closed!", 'error');
        };
        ws.onmessage = function (evt) {
         debug('response: ' + evt, 'response');
        };
    I'm guessing that the error lies in the C# server as chrome is sending the information as it should, but as a said the `onopen` function is never called.

    So my question in short:
    Has any of you ever accomplished this - and if yes, how did you do it?
    And of cause: Do you see any apparent errors in the code (hope thats not to much to ask)

  2. #2
    Join Date
    Dec 2009
    Posts
    22

    Re: websockets server trouble

    Well, i figured it out.

    If written a small article on codeproject.com about the result. Go check it out if you are interested:
    http://www.codeproject.com/KB/webser...et_server.aspx

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center