Click here to Skip to main content
15,883,868 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.1K   1.2K   41  
InsomniaServer enables you to add a fully-featured, customizable webserver to your projects. See how it works.
//All code is copyright � 2007, InsomniaSoftware | Manuel Then, All rights reserved.

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace InsomniaSoftware.Server
{
    sealed class EndPointListener : IDisposable
    {
        #region Public variables
        public IPEndPoint endPoint
        {
            get
            {
                return (IPEndPoint)socket.LocalEndPoint;
            }
        }
        #endregion

        #region Private variables
        InsomniaServer server;

        bool listening = false;
        Thread listenThread;

        Socket socket = null;
        #endregion


        public EndPointListener(InsomniaServer server, IPEndPoint localEndPoint)
        {
            if (server == null)
                throw new Exception("No server handle passed to the constructor");
            if (localEndPoint == null)
                throw new Exception("No local end point passed to the constructor");
            this.server = server;

            try
            {
                socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
                socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, true);
                socket.Bind(localEndPoint);
            }
            catch
            {
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, true);
                socket.Bind(localEndPoint);
            }
            socket.Listen(10);
        }

        public void Dispose()
        {
            Stop(500);
            if (socket != null)
            {
                if (socket.Connected)
                    socket.Disconnect(false);
                socket = null;
            }
        }


        public void Start()
        {
            lock (this)
            {
                if (!listening)
                {
                    listening = true;
                    listenThread = new Thread(ListenThread);
                    listenThread.Start();
                }
            }
        }

        public void Stop(int maxWaitTime)
        {
            try
            {
                lock(this)
                    listening = false;
                listenThread.Join(maxWaitTime);
                if (listenThread.ThreadState == ThreadState.Running)
                    listenThread.Abort();
            }
            catch { }
        }


        void ListenThread()
        {
            while (listening)
            {
                try
                {
                    while (socket.Poll(0, SelectMode.SelectRead))
                    {
                        ExtendedSocket listenSocket = ExtendedSocket.FromSocket(socket.Accept());
                        HttpConnection httpConnection = new HttpConnection(server, listenSocket);
                    }
                    Thread.Sleep(20);
                }
                catch { }
            }
        }
    }
}

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