Click here to Skip to main content
15,892,005 members
Articles / Programming Languages / XML

Using InsomniaServer to Build a Web-interface for your Application

Rate me:
Please Sign up or sign in to vote.
4.80/5 (11 votes)
15 Jul 2011CPOL3 min read 83.5K   1.2K   41  
InsomniaServer enables you to add a fully-featured, customizable webserver to your projects. See how it works.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;

using InsomniaSoftware.Server;
using InsomniaSoftware.Server.Sources;

namespace PopulateTheServer
{
    public partial class Form1 : Form
    {
        InsomniaServer server = null;

        public Form1()
        {
            InitializeComponent();

            //Create a new server instance and initialize it
            server = new InsomniaServer();

            //Get local end points and add them
            IPAddress[] addresses = Dns.GetHostAddresses("");
            foreach (IPAddress curAddress in addresses)
                server.AddEndPoint(new IPEndPoint(curAddress, 80));
            server.AddEndPoint(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 80));

            //Enable server logging
            server.log.SetLoggingStream(new FileStream("log.txt", FileMode.Append));


            //Add "/berlin.jpg"
            server.fileSystem.AddSource("photo.jpg", FileSource.GetSource, "../berlin.jpg");

            //Set berlin.jpg as the root's default page
            server.fileSystem.root.defaultPage = "photo.jpg";


            //Add the documentation
            //- you may have to change its path
            DirectorySource docuSource = new DirectorySource("../../../../../doc/");
            server.fileSystem.AddSource("docu/*", DirectorySource.GetSource, docuSource);
            server.fileSystem.GetNode("docu").defaultPage = "Index.html";  //Capital index - as Sandcastle (my help generator) has this naming convention


            //Start server
            server.Start();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            //Shut down InsomniaServer
            server.Stop();
            server.Dispose();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
Germany Germany
I was born in 1987. Unfortunately too late to experience the real rise of the PC. But fortunately late enough to enjoy things like MS's .net during my school time Wink | ;)

From the time when some relative taught me a little BASIC under MS DOS, I loved to tell computers what to do - even though my real start in programming was around the age of 16.

At the moment, I am studying Software Engineering at University of Augsburg, always hoping to find time to design and program.
Besides, I like meeting friends, spent time with my girlfriend and enjoy life Smile | :)

Comments and Discussions