Share via


Developing a Native C++ Server Application

Other versions of this page are also available for the following:

  • Windows Embedded CE 6.0 R3

8/27/2008

This section discusses the development of a sample web service on top of the framework, developed in native code and statically linked to the fake server library. This server application will send a "hello" message to its clients over HTTP. Use the application below as a template and modify it to suit your specific server needs.

Note

If you already have a project setup and just want to link it to the web server framework, then all the steps below may not be applicable. Please choose your steps accordingly.

To create a native (C++) server application

  1. Create a directory for your application.

  2. Create the sources file and copy over the makefile in the directory.

  3. Update the sources file by adding the following lines to the INCLUDES section of the sources file:

    $(_PRIVATEROOT)\qaapps\qacore\fakeserver\inc;\
    $(_PRIVATEROOT)\qaapps\qacore\fakeserver\fakeserverlib;\
    
  4. Update the sources file by adding the following lines to the TARGETLIBS section of the sources file:

    $(QALIBDIR)\fakeserverlib.lib \
    
  5. Include the following header file in your project:

    #include "Fakeserverhelper.h"
    
  6. Build your project and check the build.log. Fix any build issues before proceeding.

  7. Define the callback function as in the following sample. This sample callback function always sends down a 200 OK response to the client. The callback function holds the response generation logic and is specific to the test case or the application:

    //
    // This is a sample HTTP response send down by the server application.
    //
    char *pSampleResponse =
                "HTTP/1.1 200 OK \r\n"
                "Date: Thu, 06 Apr 2006 00:58:18 GMT\r\n"
                "Server: Microsoft-IIS/6.0\r\n"
                "Connection: close\r\n"
                "Content-Type: text/html; charset=utf-8\r\n"
                "Content-Length: 5\r\n\r\n"
                "Hello";
    
    
    HRESULT CallBackFunction (CLIENT_REQUEST *pRequest, IFakeServerResponse *pResponse, DWORD dwMetaInfo)
    {
        HRESULT    hr = S_OK;
    
        //
        // Initialize the response pointer
        //
        pResponse-> Initialize();
    
        //
        // Set the sample response
        //
        pResponse->SetResponse(pSampleResponse, strlen(pSampleResponse));
    
        return S_OK;
    }
    
  8. Invoke the server listener. After defining the call back function you must add code to invoke/start the HTTP listener on port 80. Sample code for invoking the server follows. This function or something similar should be a part of the start up routine for your application:

    HRESULT InvokeServer()
    {
        TEST_CALLBACK_FUNCTION pFn;
        HRESULT hr = S_OK;
    
        pFn = CallBackFunction;
    
        //
        // create the fake http server object
        //
        CreateFakeHttpServerObject(&g_pFakeServer);
    
        //
        // Initialize the fake server
        //
        g_pFakeServer->Initialize();
    
        //
        // Set the call back function
        //
        g_pFakeServer->SetCallBack(pFn);
    
        //
        // Start the server
        //
        g_pFakeServer->Start();
    
        return hr;
    }
    
  9. Define the routine for stopping the server. The following sample will stop the web server:

    HRESULT StopWebServer
    {
        HRESULT hr = S_OK;
    
        //
    // Stop the web server
        //
        g_pFakeServer->Stop();
    
        return hr;
    }
    
  10. Invoke the above-defined function from the main routine:

    CComPtr<IFakeServer>    g_pFakeServer;
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int iCmdShow)
    {
        HRESULT hr = S_OK;
    
        if (FAILED(CoInitializeEx(0, COINIT_MULTITHREADED)))
        {
            return -2;
        }
    
        InvokeServer();
    
        while(1)
        {
            //
            // Sleep for a while
            //
            Sleep(10000);
        }
    
        StopServer();
    
        CoUninitialize();
        return 1;
    }
    
  11. Build and run your executable.

  12. Open up Internet Explorer Mobile and type https://localhost and you will see the server responding with a “hello” message.

See Also

Other Resources

Developing Server Applications with the Local Server Framework