Click here to Skip to main content
15,888,521 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.2K   1.2K   41  
InsomniaServer enables you to add a fully-featured, customizable webserver to your projects. See how it works.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

	<head>
		<meta http-equiv="content-type" content="text/html;charset=utf-8">
		<meta name="generator" content="Adobe GoLive">
		<title>InsomniaServer - Tutorial 1</title>
		<style><!--
			.code {
				word-wrap:break-word;
				margin:10px;
				padding:10px;
				border:2px ridge white;
				background-color:#eeeeee;
				font-family:Courier New;
				font-size:10pt;
				}
				
			.step { font-weight: bold; background-color: #dcdcdc; font-style: italic; margin: 1px 2px; padding: 1px 2px; border: solid 1px #898989; }
		--></style>
	</head>

	<body>
		<p><strong><font size="+2">Tutorial 1 - Initializing InsomniaServer</font></strong></p>
		<p>In this first lesson of our trip through some of the InsomniaServer possibilities we will learn how to initialize it, which is really simple. All of my tutorials will be in C#, but it should be quite easy to translate it to other .NET languages.</p>
		<p><em>The final code and the resulting assemblies are stored in &quot;Examples/Tutorial 1 - InizializeIS/&quot;</em></p>
		<p><span class="step">Step one:</span> Create a new Windows application within VisualStudio or any other IDE of your choice. I called this project <em>InitialzeIS</em>. The code generator does his work and creates something like this:</p>
		<div class="code">
			<font color="blue">using </font><font color="black">System</font><font color="blue">;<br>
				using </font><font color="black">System.Collections.Generic</font><font color="blue">;<br>
				using </font><font color="black">System.ComponentModel</font><font color="blue">;<br>
				using </font><font color="black">System.Data</font><font color="blue">;<br>
				using </font><font color="black">System.Drawing</font><font color="blue">;<br>
				using </font><font color="black">System.Text</font><font color="blue">;<br>
				using </font><font color="black">System.Windows.Forms</font><font color="blue">;<br>
				<br>
				namespace </font><font color="black">InitializeIS<br>
				{<br>
				    </font><font color="blue">public </font><font color="black">partial </font><font color="blue">class </font><font color="black">Form1 : Form<br>
				    {<br>
				        </font><font color="blue">public </font><font color="black">Form1()<br>
				        {<br>
				            InitializeComponent()</font><font color="blue">;<br>
				        </font><font color="black">}<br>
				    }<br>
				}<br>
			</font></div>
		<p>As you probably recognize, this is - among some other generated files - the base of a Windows forms application.</p>
		<br>
		<p><span class="step">Step two:</span> Reference InsomniaSoftware.dll within the example project. This is done by clicking <em>References - Add reference - Browse</em>. In that list you go to the path where you store the InsomniaServer.dll, select it and click <em>OK</em>.</p>
		<p>Now add the following lines to the source file:</p>
		<div class="code">
			<font color="blue">using </font><font color="black">System.Net</font><font color="blue">;<br>
				using </font><font color="black">System.IO</font><font color="blue">;<br>
				using </font><font color="black">InsomniaSoftware.Server</font><font color="blue">;</font></div>
		<p>Finally, we are able to use InsomniaServer.</p>
		<br>
		<p><span class="step">Step three:</span> First, add a private variable for the server to our class <em>Form1</em>:</p>
		<div class="code">
			<font color="black">InsomniaServer server </font><font color="blue">= null;</font></div>
		<p>And now initialize it in the form's constructor:</p>
		<div class="code">
			<font color="darkgreen">//Create a new server instance and initialize it<br>
			</font><font color="black">server </font><font color="blue">= new </font><font color="black">InsomniaServer()</font><font color="blue">;</font></div>
		<br>
		<p><span class="step">Step four:</span> To let the server know where (at which EndPoints) it is to listen for incoming connections, we have to add them. For this example I decided to add all addresses bound to this machine plus the <em>localhost</em>.</p>
		<div class='code'>
<font color='darkgreen'>//Get&nbsp;local&nbsp;end&nbsp;points&nbsp;and&nbsp;add&nbsp;them<br />
</font><font color='black'>IPAddress[]&nbsp;addresses&nbsp;</font><font color='blue'>=&nbsp;</font><font color='black'>Dns.GetHostAddresses(</font><font color='#808080'>""</font><font color='black'>)</font><font color='blue'>;<br />
foreach&nbsp;</font><font color='black'>(IPAddress&nbsp;curAddress&nbsp;</font><font color='blue'>in&nbsp;</font><font color='black'>addresses)<br />
&nbsp;&nbsp;&nbsp;&nbsp;server.AddEndPoint(</font><font color='blue'>new&nbsp;</font><font color='black'>IPEndPoint(curAddress,&nbsp;</font><font color='maroon'>80</font><font color='black'>))</font><font color='blue'>;<br />
</font><font color='black'>server.AddEndPoint(</font><font color='blue'>new&nbsp;</font><font color='black'>IPEndPoint(IPAddress.Parse(</font><font color='#808080'>"127.0.0.1"</font><font color='black'>),&nbsp;</font><font color='maroon'>80</font><font color='black'>))</font><font color='blue'>;</font>
	</div>		<br>
		<p><span class="step">Step five (optional):</span> Log file creation is not enabled by default. If you want InsomniaServer to log every request and some special actions add the following lines to your code:</p>
		<div class="code">
			<font color="darkgreen">//Enable server logging<br>
			</font><font color="black">server.log.SetLoggingStream(</font><font color="blue">new </font><font color="black">FileStream(</font><font color="#808080">&quot;log.txt&quot;</font><font color="black">, FileMode.Append))</font><font color="blue">;</font></div>
		<p>A final log file looks like this:</p>
		<div class="code">
			###################################################################<br>
			Log opened 21:30:21 - Dienstag, 13. Februar 2007<br>
			InsomniaServer/0.01a<br>
			Source added at &quot;/*&quot;<br>
			Source added at &quot;/doc/*&quot;<br>
			Source added at &quot;/test/index.html&quot;<br>
			Source added at &quot;/test/getResponse.html&quot;<br>
			[21:30:21-13.02.2007] Server started<br>
			@ 192.168.2.106:80<br>
			@ 192.168.38.1:80<br>
			@ 192.168.59.1:80<br>
			[21:30:45-13.02.2007] 192.168.38.1:4809 &lt;- &quot;GET /pesta/index.html?a=%26%231234%3Bbca HTTP/1.1&quot;@&quot;192.168.38.1:80&quot; - 200 4551b (13,7637977521375sec) -&gt; &quot;Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1; .NET CLR 3.0.04506.30)&quot;<br>
			[21:35:22-13.02.2007] 192.168.38.1:4815 &lt;- &quot;GET /pesta/index.html?a=%26%231234%3Bbca HTTP/1.1&quot;@&quot;192.168.38.1:80&quot; - 304 207b (3,29070773782658sec) -&gt; &quot;Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1; .NET CLR 3.0.04506.30)&quot;<br>
			[21:35:23-13.02.2007] 192.168.38.1:4823 &lt;- &quot;GET /pesta/Dateien/dtree.css HTTP/1.1&quot;@&quot;192.168.38.1:80&quot; - 304 205b (0,097271162873792sec) -&gt; &quot;Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1; .NET CLR 3.0.04506.30)&quot;<br>
			[21:35:23-13.02.2007] 192.168.38.1:4825 &lt;- &quot;GET /pesta/Dateien/bkg.gif HTTP/1.1&quot;@&quot;192.168.38.1:80&quot; - 304 205b (0,277489783036364sec) -&gt; &quot;Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1; .NET CLR 3.0.04506.30)&quot;<br>
			[21:35:23-13.02.2007] 192.168.38.1:4827 &lt;- &quot;GET /pesta/Dateien/head.gif HTTP/1.1&quot;@&quot;192.168.38.1:80&quot; - 304 208b (0,25088182252301sec) -&gt; &quot;Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1; .NET CLR 3.0.04506.30)&quot;<br>
			[21:35:23-13.02.2007] 192.168.38.1:4829 &lt;- &quot;GET /pesta/Dateien/menu2.gif HTTP/1.1&quot;@&quot;192.168.38.1:80&quot; - 304 207b (0,14275068786719sec) -&gt; &quot;Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1; .NET CLR 3.0.04506.30)&quot;<br>
			[21:36:21-13.02.2007] Server stopped<br>
			Log closed 21:36:22 - Dienstag, 13. Februar 2007<br>
			###################################################################</div>
		<br>
		<p><span class="step">Step six:</span> Finally, we can start the server : ) Guess how ...:</p>
		<div class="code">
			<font color="black">server.Start()</font><font color="blue">;</font></div>
		<p>Easy, isn't it?</p>
		<br>
		<p><span class="step">Step seven:</span> As we are very orderly, a final task is to clean everything up, when the application shuts down. Just add an event handler for the <em>FormClosed</em> event and insert the following code:</p>
		<div class="code">
			<font color="blue">private void </font><font color="black">Form1_FormClosed(</font><font color="blue">object </font><font color="black">sender, FormClosedEventArgs e)<br>
				{<br>
			</font><font color="darkgreen">&nbsp;&nbsp;&nbsp;&nbsp;//Shut down InsomniaServer<br>
			</font><font color="black">&nbsp;&nbsp;&nbsp;&nbsp;server.Stop()</font><font color="blue">;<br>
			</font><font color="black">&nbsp;&nbsp;&nbsp;&nbsp;server.Dispose()</font><font color="blue">;<br>
			</font><font color="black">}</font></div>
		<p></p>
		<p>The moment all of us are awaiting has come. Compile the project and enjoy.<br>If you run a firewall, you will see a security warning that our sample wants to accept incoming connections.<br>
			Unfortunately there is nothing more to see except the created &quot;log.txt&quot; (and a debugging window if you run the StatisticsForm version of InsomniaSoftware.dll).</p>
		<p>But I promise, in the following tutorials there will be more ; )</p>
	</body>

</html>

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