<!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 "Examples/Tutorial 1 - InizializeIS/"</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 local end points and add them<br />
</font><font color='black'>IPAddress[] addresses </font><font color='blue'>= </font><font color='black'>Dns.GetHostAddresses(</font><font color='#808080'>""</font><font color='black'>)</font><font color='blue'>;<br />
foreach </font><font color='black'>(IPAddress curAddress </font><font color='blue'>in </font><font color='black'>addresses)<br />
server.AddEndPoint(</font><font color='blue'>new </font><font color='black'>IPEndPoint(curAddress, </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 </font><font color='black'>IPEndPoint(IPAddress.Parse(</font><font color='#808080'>"127.0.0.1"</font><font color='black'>), </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">"log.txt"</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 "/*"<br>
Source added at "/doc/*"<br>
Source added at "/test/index.html"<br>
Source added at "/test/getResponse.html"<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 <- "GET /pesta/index.html?a=%26%231234%3Bbca HTTP/1.1"@"192.168.38.1:80" - 200 4551b (13,7637977521375sec) -> "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)"<br>
[21:35:22-13.02.2007] 192.168.38.1:4815 <- "GET /pesta/index.html?a=%26%231234%3Bbca HTTP/1.1"@"192.168.38.1:80" - 304 207b (3,29070773782658sec) -> "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)"<br>
[21:35:23-13.02.2007] 192.168.38.1:4823 <- "GET /pesta/Dateien/dtree.css HTTP/1.1"@"192.168.38.1:80" - 304 205b (0,097271162873792sec) -> "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)"<br>
[21:35:23-13.02.2007] 192.168.38.1:4825 <- "GET /pesta/Dateien/bkg.gif HTTP/1.1"@"192.168.38.1:80" - 304 205b (0,277489783036364sec) -> "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)"<br>
[21:35:23-13.02.2007] 192.168.38.1:4827 <- "GET /pesta/Dateien/head.gif HTTP/1.1"@"192.168.38.1:80" - 304 208b (0,25088182252301sec) -> "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)"<br>
[21:35:23-13.02.2007] 192.168.38.1:4829 <- "GET /pesta/Dateien/menu2.gif HTTP/1.1"@"192.168.38.1:80" - 304 207b (0,14275068786719sec) -> "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)"<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"> //Shut down InsomniaServer<br>
</font><font color="black"> server.Stop()</font><font color="blue">;<br>
</font><font color="black"> 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 "log.txt" (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>