title: POCO 1.5.1 Websocket client unable to connect to c# websocket server
body: <p>Problem:</p>
  
  <p>A websocket client(POCO 1.5.1, c++) will not connect to a websocket c# server (command line app with Fleck Library). A <strong>timeout</strong> is reached with an exception thrown:</p>
  
  <pre><code>Cannot upgrade to WebSocket connection: OK
  Exception Code: 1
  
  Poco WebException Documentation
  WS_ERR_NO_HANDSHAKE = 1 : 
  No Connection: Upgrade or Upgrade: websocket header in handshake request.
  </code></pre>
  
  <p><em>Fact 1</em>: This <strong>websocket client</strong> will connect to a <strong>Ruby Event Machine</strong> websocket server.</p>
  
  <p><em>Fact 2</em>: A <strong>javascript client</strong> will connect to the <strong>websocket c# server</strong>.</p>
  
  <p><em>Fact 3</em>: The same <strong>javascript client</strong> will also connect to the <strong>websocket ruby server</strong>.</p>
  
  <p><em>Fact 4</em>: The <strong>websocket client</strong> will not connect to an <strong>Alchemy Websocket server</strong> neigther. <a href="https://github.com/Olivine-Labs/Alchemy-Websockets" rel="nofollow">https://github.com/Olivine-Labs/Alchemy-Websockets</a></p>
  
  <p>Update with Wireshark 
  POCO is using
  <strong>GET / HTTP/1.0\r\n</strong></p>
  
  <p>Javascript version:
  <strong>GET / HTTP/1.1\r\n</strong></p>
  
  <hr>
  
  <h1>All Source Code</h1>
  
  <h2>Client code in c++</h2>
  
  <pre><code>#include "Game.h"
  
  #include &lt;irrlicht.h&gt;
  #include "driverChoice.h"
  #include &lt;iostream&gt;
  #include &lt;assert.h&gt;
  
  #include "Poco/Net/WebSocket.h"
  #include "Poco/Net/HTTPClientSession.h"
  #include "Poco/Net/HTTPRequest.h"
  #include "Poco/Net/HTTPResponse.h"
  #include "Poco/Net/ServerSocket.h"
  #include "Poco/Net/NetException.h"
  #include "Poco/Exception.h"
  
  using Poco::Net::HTTPClientSession;
  using Poco::Net::HTTPRequest;
  using Poco::Net::HTTPResponse;
  using Poco::Net::HTTPServerRequest;
  using Poco::Net::HTTPServerResponse;
  using Poco::Net::WebSocket;
  using Poco::Net::WebSocketException;
  using Poco::Exception;
  
  // VS2010
  // POCO 1.5.1
  // Irrlicht 3D engine
  // Windows 7 Enterprise edition
  
  Game::Game(void)
  {
  }
  
  Game::~Game(void)
  {
  }
  
  //...
  
  void Game::TestWebSocketClient()
  {
    char buffer[1024];
    int flags;
    int n;
    std::string payload;
  
    try
    {
        HTTPClientSession cs("localhost", 8080);
        HTTPRequest request(HTTPRequest::HTTP_GET, "/ws");
        HTTPResponse response;
        std::string cmd;
  
        WebSocket * ws = new WebSocket(cs, request, response); // Causes the timeout
  
        payload = "SGClient: Hello World!";
        ws-&gt;sendFrame(payload.data(), payload.size(), WebSocket::FRAME_TEXT);
        n = ws-&gt;receiveFrame(buffer, sizeof(buffer), flags);
  
        while( cmd != "exit")
        {
          cmd = "";
          std::cin &gt;&gt; cmd;
          ws-&gt;sendFrame(cmd.data(), cmd.size(), WebSocket::FRAME_TEXT);
          n = ws-&gt;receiveFrame(buffer, sizeof(buffer), flags);
          if( n &gt; 0 )
          {
              std::cout &lt;&lt; buffer &lt;&lt; std::endl;
          }
        }
  
        ws-&gt;shutdown();
    }
    catch (Exception ex)
    {
        return;
    }
  </code></pre>
  
  <h2>Server code in c#</h2>
  
  <pre><code>// vs2010
  // fleck library
  // Windows 7 enterprise edition
  
  
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Collections;
  using Fleck;
  using System.Timers;
  
  namespace TestWebsocket
  {
      class Program
      {
          static void Main()
          {
              FleckLog.Level = LogLevel.Debug;
              var allSockets = new List&lt;IWebSocketConnection&gt;();
              var server = new WebSocketServer("ws://localhost:8080");
              server.Start(socket =&gt;
              {
                  socket.OnOpen = () =&gt;
                  {
                      Console.WriteLine("Open!");
                      allSockets.Add(socket);
                  };
                  socket.OnClose = () =&gt;
                  {
                      Console.WriteLine("Close!");
                      allSockets.Remove(socket);
                  };
                  socket.OnMessage = message =&gt;
                  {
                      Console.WriteLine(message);
                      allSockets.ToList().ForEach(s =&gt; s.Send("Echo: " + message));
                  };
              });
  
              var input = Console.ReadLine();
              while (input != "exit")
              {
                  foreach (var socket in allSockets.ToList())
                  {
                      socket.Send(input);
                  }
                  input = Console.ReadLine();
              }
  
          }
      }
  }
  </code></pre>
  
  <h2>Alternate HTML/Javascript client code, running in Chrome Version 31.0.1650.57 m</h2>
  
  <pre><code>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;websocket client&lt;/title&gt;
      &lt;script type="text/javascript"&gt;
          var start = function () {
              var inc = document.getElementById('incomming');
              var wsImpl = window.WebSocket || window.MozWebSocket;
              var form = document.getElementById('sendForm');
              var input = document.getElementById('sendText');
  
              inc.innerHTML += "connecting to server ..&lt;br/&gt;";
  
              // create a new websocket and connect
              window.ws = new wsImpl('ws://localhost:8080/');
  
              // when data is comming from the server, this metod is called
              ws.onmessage = function (evt) {
                  inc.innerHTML += evt.data + '&lt;br/&gt;';
              };
  
              // when the connection is established, this method is called
              ws.onopen = function () {
                  inc.innerHTML += '.. connection open&lt;br/&gt;';
              };
  
              // when the connection is closed, this method is called
              ws.onclose = function () {
                  inc.innerHTML += '.. connection closed&lt;br/&gt;';
              }
  
              form.addEventListener('submit', function(e){
                  e.preventDefault();
                  var val = input.value;
                  ws.send(val);
                  input.value = "";
              });
  
          }
          window.onload = start;
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="sendForm"&gt;
          &lt;input id="sendText" placeholder="Text to send" /&gt;
      &lt;/form&gt;
      &lt;pre id="incomming"&gt;&lt;/pre&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <h2>Alternate ruby server code using eventmachine running on command line interface</h2>
  
  <pre><code>// Ruby 1.9
  // gem install em-websocket required.
  require 'em-websocket'
  
  EventMachine::WebSocket.start(:host =&gt; "localhost", :port =&gt; 8080) do |ws|
    ws.onopen    { ws.send "RS: Hello Client!"}
    ws.onmessage { 
              |msg| ws.send "RS: Pong: #{msg}" 
              puts msg 
             }
    ws.onclose   { puts "WebSocket closed" }
  end
  </code></pre>