<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Kevin Grad &#187; Programming</title>
	<atom:link href="http://couchware.ca/www/kev/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://couchware.ca/www/kev</link>
	<description>Designer, Programmer and Co-Founder</description>
	<lastBuildDate>Sat, 05 Feb 2011 20:51:24 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Coding a web server with C# &#8211; part 2</title>
		<link>http://couchware.ca/www/kev/2011/02/05/coding-a-web-server-with-c-part-2/</link>
		<comments>http://couchware.ca/www/kev/2011/02/05/coding-a-web-server-with-c-part-2/#comments</comments>
		<pubDate>Sat, 05 Feb 2011 20:48:11 +0000</pubDate>
		<dc:creator>kev</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[web-server]]></category>

		<guid isPermaLink="false">http://couchware.ca/www/kev/?p=269</guid>
		<description><![CDATA[Hey all,
We left off at the end of my last article being able to grab and print a GET request from a web page. There were a few issues with the way we did things however. Firstly, we were reading in a buffer the size of the maximum possible buffer size, so when we were [...]]]></description>
			<content:encoded><![CDATA[<p>Hey all,</p>
<p>We left off at the end of my last article being able to grab and print a GET request from a web page. There were a few issues with the way we did things however. Firstly, we were reading in a buffer the size of the maximum possible buffer size, so when we were printing out our request it was followed by a ton of whitespace. In order to fix this I tried to trim the string for whitespace, this did not work.<br />
<span id="more-269"></span></p>
<p>It turns out that if you take a look at the ASCII characters of the string you get in the byte buffer you will notice that there are a ton of 0&#8217;s at the end of the string. This corresponds to the null character. When Receive() grabs data from the socket, it will fill up the remainder of the array with null characters, so to trim the string we simply use</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:Consolas, monospace;">    convertedData <span style="color: #008000;">=</span> convertedData.<span style="color: #0000FF;">Trim</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">'<span style="color: #008080; font-weight: bold;">\0</span>'</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Our next issue is how to parse the request we get. Basically all we want to do is find out what page/resource they are looking for and then check to see if that page exists, if it does respond with it, otherwise respond with nothing. However, there is a special case we have to look for. When a person browses to a site like www.blah.com the first get request is always going to look like &#8220;GET / http/1.1&#8243;, followed by the rest of the GET request. What this means is that the browser is looking for the &#8220;/&#8221; file or resource. By convention this &#8220;/&#8221; is always index.html (unless otherwise defined) so we have to check for this in our server. </p>
<p>So I created a method to parse our get that just looks at the first line of the request and returns the proper resource.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:Consolas, monospace;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> ParseGet<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">String</span> getRequest<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// Set the directory to server from to desktop\sites.</span>
    var path <span style="color: #008000;">=</span> Environment.<span style="color: #0000FF;">GetFolderPath</span><span style="color: #000000;">&#40;</span>Environment.<span style="color: #0000FF;">SpecialFolder</span>.<span style="color: #0000FF;">DesktopDirectory</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\\</span>sites&quot;</span><span style="color: #008000;">;</span>
    var words <span style="color: #008000;">=</span> getRequest.<span style="color: #0000FF;">Split</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>words<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Equals</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;GET&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>words<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Equals</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;/&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #008000;">!</span>File.<span style="color: #0000FF;">Exists</span><span style="color: #000000;">&#40;</span>path <span style="color: #008000;">+</span> words<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">?</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">:</span> File.<span style="color: #0000FF;">ReadAllBytes</span><span style="color: #000000;">&#40;</span>path <span style="color: #008000;">+</span> words<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">else</span>
    <span style="color: #000000;">&#123;</span>
        var bytes <span style="color: #008000;">=</span> File.<span style="color: #0000FF;">ReadAllBytes</span><span style="color: #000000;">&#40;</span>path<span style="color: #008000;">+</span><span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\\</span>index.html&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">return</span> bytes<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Our final problem is that currently, we can only accept one request at a time. We listen on the port until a request is made, then we process that request until completion, then we listen again. We want to be able to handle multiple requests concurrently, so we must thread our request procession. To do this, we move our request processing out into its own method:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:Consolas, monospace;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> ProcessRequest<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">Object</span> o<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    var acceptedSocket <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>Socket<span style="color: #000000;">&#41;</span> o<span style="color: #008000;">;</span>
    <span style="color: #008080; font-style: italic;">// Read request.</span>
    var data <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span>acceptedSocket.<span style="color: #0000FF;">ReceiveBufferSize</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
    acceptedSocket.<span style="color: #0000FF;">Receive</span><span style="color: #000000;">&#40;</span>data<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    var convertedData <span style="color: #008000;">=</span> Encoding.<span style="color: #0000FF;">ASCII</span>.<span style="color: #0000FF;">GetString</span><span style="color: #000000;">&#40;</span>data<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// Receive() fills the buffer with nulls after the data has been read.</span>
    convertedData <span style="color: #008000;">=</span> convertedData.<span style="color: #0000FF;">Trim</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">'<span style="color: #008080; font-weight: bold;">\0</span>'</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #FF0000;">char</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> delimiters <span style="color: #008000;">=</span> <span style="color: #000000;">&#123;</span> <span style="color: #666666;">'<span style="color: #008080; font-weight: bold;">\r</span>'</span>, <span style="color: #666666;">'<span style="color: #008080; font-weight: bold;">\n</span>'</span> <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
    var lines <span style="color: #008000;">=</span> convertedData.<span style="color: #0000FF;">Split</span><span style="color: #000000;">&#40;</span>delimiters, StringSplitOptions.<span style="color: #0000FF;">RemoveEmptyEntries</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;{0}&quot;</span>, convertedData<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    var outData <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// Write response.</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// Make sure there is data to parse.</span>
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span> <span style="color: #008000;">!=</span> lines.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        outData <span style="color: #008000;">=</span> ParseGet<span style="color: #000000;">&#40;</span>lines<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    acceptedSocket.<span style="color: #0000FF;">Send</span><span style="color: #000000;">&#40;</span>outData<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    acceptedSocket.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>You will notice from the above code that ProcessRequest takes an Object instead of a socket. This is too bad, it would be nice if you could do that<br />
ParameterizedThreadStart requires a method that takes an object and I could find no way around this.</p>
<p>Our final run method now looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:Consolas, monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Run<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// Set up socket for listening.</span>
    var hostIp <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>Dns.<span style="color: #0000FF;">GetHostAddresses</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;127.0.0.1&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Listening on {0}:{1}&quot;</span>, hostIp, Port<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    var endPoint <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> IPEndPoint<span style="color: #000000;">&#40;</span>hostIp, Port<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    TcpSocket.<span style="color: #0000FF;">Bind</span><span style="color: #000000;">&#40;</span>endPoint<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        TcpSocket.<span style="color: #0000FF;">Listen</span><span style="color: #000000;">&#40;</span>BackLog<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        var processThread <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Thread<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> ParameterizedThreadStart<span style="color: #000000;">&#40;</span>ProcessRequest<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        processThread.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span>TcpSocket.<span style="color: #0000FF;">Accept</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>If you create a directory on your desktop, place an index.html in there and then navigate to http://127.0.0.1 it will now<br />
serve your very own index.html. Here is mine:<br />
<img src="http://couchware.ca/users/kevin/index.png" alt="Index!" /><br />
If you would like to check out the code, it is available here: <a href="http://code.google.com/p/kevserver/">KevServer</a></p>
]]></content:encoded>
			<wfw:commentRss>http://couchware.ca/www/kev/2011/02/05/coding-a-web-server-with-c-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Coding a web server with C# &#8211; part 1</title>
		<link>http://couchware.ca/www/kev/2011/02/01/coding-a-web-server-with-c-part-1/</link>
		<comments>http://couchware.ca/www/kev/2011/02/01/coding-a-web-server-with-c-part-1/#comments</comments>
		<pubDate>Wed, 02 Feb 2011 02:30:54 +0000</pubDate>
		<dc:creator>kev</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[web-server]]></category>

		<guid isPermaLink="false">http://couchware.ca/www/kev/?p=220</guid>
		<description><![CDATA[Howdy folks,
It has been quite a while since I programmed something productive on my own time. I have been really busy lately at work as we are finishing up our major project from the last 6 months. Hopefully at some point I will write a blog post regarding that project as it is really interesting. [...]]]></description>
			<content:encoded><![CDATA[<p>Howdy folks,</p>
<p>It has been quite a while since I programmed something productive on my own time. I have been really busy lately at work as we are finishing up our major project from the last 6 months. Hopefully at some point I will write a blog post regarding that project as it is really interesting. I have also been reading up on some languages I have toyed around with (Javascript, Objective-C, C#, Clojure). I also haven&#8217;t updated this blog in months. Because of these facts I have decided that it is time to stop reading and start doing, so I have come up with a fun little project for myself.<br />
<span id="more-220"></span></p>
<p>Yesterday I was in my idea chamber (shower) and I came up with a fun little idea that shouldn&#8217;t be too hard and will teach me a bit about two subjects I would like to know more about. I have decided to write my own little web server from scratch using C#. This will teach me more about C# and its libraries as well a lot more about the HTTP/1.1 protocol.</p>
<p>Now I know that C# comes with some nice libraries that makes writing things like web servers pretty easy/trivial, so I have decided not to use any of them. I will not be using HTTPWebRequest or HTTPWebResponse. I will be doing this using straight sockets and a design from my own head, as well as a lot of help from MSDN and the HTTP spec.</p>
<p>The second part of this journey will be to blog about my exploits so you guys can critique, offer suggestions, bash my designs or praise my awesomeness. This way I can actually tackle three birds with one stone as in addition to learning C# and HTTP I will practice my writing by blogging about it. So without further ado I will take you through my initial steps. If anyone is interested all my code is available at <a href="http://code.google.com/p/kevserver">Google Code</a> and will be updated as I go.</p>
<p>The first step I set out to achieve is to create a socket that will listen to a single request on a specific port, pretty basic. However, being a C# beginner I had no idea what to do, so I did what any competent programmer would do, I Googled &#8220;C# sockets&#8221; as well as &#8220;Http&#8221; and some other related stuff. I quickly came upon the MSDN article on the Socket class located in System.Net.Sockets.</p>
<p>As an aside, I have to say, this is the first time I have ever used MSDN and it is pretty damn good. Not only does it give you the full API for whatever you are looking for, it also provides you with code snippets on how to use it! INVALUABLE! Much much better resource than the JavaDocs I am used to. I guess that is what you get when you have professional support for your API&#8230; I would probably still prefer the API to be open source but I digress. Anyway, I quickly found what I was looking for (Socket) and set to work. After about an hour of mucking about I finally achieved some code that actually works and displayed a single get request from my browser. Behold my work of art:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:Consolas, monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Linq</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Net</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Net.Sockets</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> KevServer
<span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">class</span> Server
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">int</span> Port <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">private</span> Socket TcpSocket <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">int</span> BackLog <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> Server<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            TcpSocket <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Socket<span style="color: #000000;">&#40;</span>AddressFamily.<span style="color: #0000FF;">InterNetwork</span>,
                SocketType.<span style="color: #0000FF;">Stream</span>, ProtocolType.<span style="color: #0000FF;">Tcp</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Port <span style="color: #008000;">=</span> <span style="color: #FF0000;">8080</span><span style="color: #008000;">;</span>
            BackLog <span style="color: #008000;">=</span> <span style="color: #FF0000;">5</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Run<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            var hostIp <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>Dns.<span style="color: #0000FF;">GetHostAddresses</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;127.0.0.1&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Listening on {0}:{1}&quot;</span>, hostIp, Port<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            var endPoint <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> IPEndPoint<span style="color: #000000;">&#40;</span>hostIp, Port<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            TcpSocket.<span style="color: #0000FF;">Bind</span><span style="color: #000000;">&#40;</span>endPoint<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            TcpSocket.<span style="color: #0000FF;">Listen</span><span style="color: #000000;">&#40;</span>BackLog<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            var acceptedSocket <span style="color: #008000;">=</span> TcpSocket.<span style="color: #0000FF;">Accept</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            var data <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span>acceptedSocket.<span style="color: #0000FF;">ReceiveBufferSize</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
            acceptedSocket.<span style="color: #0000FF;">Receive</span><span style="color: #000000;">&#40;</span>data, <span style="color: #FF0000;">0</span>,
                acceptedSocket.<span style="color: #0000FF;">ReceiveBufferSize</span>, SocketFlags.<span style="color: #0000FF;">None</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            var convertedData <span style="color: #008000;">=</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Text</span></span>.<span style="color: #0000FF;">Encoding</span>.<span style="color: #0000FF;">ASCII</span>.<span style="color: #0000FF;">GetString</span><span style="color: #000000;">&#40;</span>data<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;{0}&quot;</span>, convertedData<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            Console.<span style="color: #0000FF;">Read</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>When opening up my browser and pointing it to 127.0.0.1:8080 my console printed the following:<br />
<img src="http://couchware.ca/users/kevin/get-request.png" alt="GET!" /></p>
<p>Great Success! First hurdle achieved, next step: parsing the request and returning a page.<br />
More to come, stay tuned!</p>
]]></content:encoded>
			<wfw:commentRss>http://couchware.ca/www/kev/2011/02/01/coding-a-web-server-with-c-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Comment from today&#8217;s code.</title>
		<link>http://couchware.ca/www/kev/2009/09/24/comment-from-todays-code/</link>
		<comments>http://couchware.ca/www/kev/2009/09/24/comment-from-todays-code/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 01:39:53 +0000</pubDate>
		<dc:creator>kev</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Wezzle]]></category>
		<category><![CDATA[comment]]></category>
		<category><![CDATA[lwjgl]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/kev/?p=165</guid>
		<description><![CDATA[

// WARNING: the ctrl mask randomly subtracts 96 from the ascii
// of any character returned from getEventCharacter(). This is because
// LWJGL is fucked. We had a 30 min convo about this and decided
// to fuck the ctrl key. Alt is better.

]]></description>
			<content:encoded><![CDATA[<pre>

// WARNING: the ctrl mask randomly subtracts 96 from the ascii
// of any character returned from getEventCharacter(). This is because
// LWJGL is fucked. We had a 30 min convo about this and decided
// to fuck the ctrl key. Alt is better.
</pre>
]]></content:encoded>
			<wfw:commentRss>http://couchware.ca/www/kev/2009/09/24/comment-from-todays-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wezzle Collision Generator</title>
		<link>http://couchware.ca/www/kev/2009/03/13/wezzle-collision-generator/</link>
		<comments>http://couchware.ca/www/kev/2009/03/13/wezzle-collision-generator/#comments</comments>
		<pubDate>Sat, 14 Mar 2009 04:10:25 +0000</pubDate>
		<dc:creator>kev</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Wezzle]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/kev/?p=124</guid>
		<description><![CDATA[I have a fascination with programming languages. I have been reading up on many different ones trying to get a feel for them all. I enjoy learning about the different syntaxes and what the advantages are of the different paradigms. I find most of the languages offer really interesting concepts and benefits.

Last night I was [...]]]></description>
			<content:encoded><![CDATA[<p>I have a fascination with programming languages. I have been reading up on many different ones trying to get a feel for them all. I enjoy learning about the different syntaxes and what the advantages are of the different paradigms. I find most of the languages offer really interesting concepts and benefits.<br />
<span id="more-124"></span></p>
<p>Last night I was hacking around with <a href="http://www.ruby-lang.org/en/">Ruby</a> and decided that to teach myself how to program better I would do something interesting for Wezzle. I had the idea of taking the <a href="http://couchware.ca/blogs/kev/2009/01/27/wezzle-xml/">XML markup for our achievements</a> and making some sort of generator using our achievement pseudo-language.</p>
<p>Our achievement language basically amounts to what is known in computing as a <a href="http://en.wikipedia.org/wiki/Domain-specific_language">Domain Specific Language (DSL).</a> Luckily for me Ruby is a pretty good language for writing DSL&#8217;s. </p>
<p>I decided to start with the achievements that I find to be the hardest to write, COLLISION. A COLLISION occurs between 1 or many items. There are two seperate types of collisions which can be defined as either an AND or an INTO relationship. These can be seen in the following two examples:</p>
<p>Example 1:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:Consolas, monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;achievement</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;A Tale of Two Rockets&quot;</span> <span style="color: #000066;">difficulty</span>=<span style="color: #ff0000;">&quot;SILVER&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Fire a rocket into another rocket.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;rule</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;COLLISION&quot;</span> <span style="color: #000066;">operation</span>=<span style="color: #ff0000;">&quot;BETWEEN&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;ROCKET&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;ROCKET&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/rule<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/achievement<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Example 2:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:Consolas, monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;achievement</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;A Tale of Two Rockets&quot;</span> <span style="color: #000066;">difficulty</span>=<span style="color: #ff0000;">&quot;SILVER&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Fire a rocket into another rocket.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;rule</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;COLLISION&quot;</span> <span style="color: #000066;">operation</span>=<span style="color: #ff0000;">&quot;BETWEEN&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;ROCKET&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;ROCKET&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/rule<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/achievement<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>While these two examples may look the same, they mean two different things. The first one can be written as COLLISION BETWEEN ROCKET AND ROCKET simply means that two rockets will be activated in a single turn. The second one however would be written like COLLISION BETWEEN ROCKET INTO ROCKET and means a rocket will be fired and activate another rocket by hitting it.</p>
<p>I have written a generator in ruby that takes an achievement (currently only collisions) in the following form:</p>
<p>BEGIN COLLISION<br />
achievement:Rocketeer:BRONZE<br />
description:Fire a rocket into a rocket and fire a rocket and a rocket.<br />
rule:COLLISION BETWEEN ROCKET INTO ROCKET<br />
rule:COLLISION BETWEEN ROCKET AND ROCKET<br />
END</p>
<p>This is currently defined within the program and will output the properly formatted and tabbed XML which you can then paste into your achievements.xml file. The goal is to have it work for all achievements, and I will keep you updated as to the progress should I choose to continue. As it stands, you can find the current version <a href="http://couchware.ca/blogs/WezzleCollisionGen.rb">here </a>although it&#8217;s a little bit ugly. Please note, you must have ruby 1.9+ in order to run it.</p>
]]></content:encoded>
			<wfw:commentRss>http://couchware.ca/www/kev/2009/03/13/wezzle-collision-generator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Performance still matters.</title>
		<link>http://couchware.ca/www/kev/2009/03/10/performance-still-matters/</link>
		<comments>http://couchware.ca/www/kev/2009/03/10/performance-still-matters/#comments</comments>
		<pubDate>Tue, 10 Mar 2009 16:08:35 +0000</pubDate>
		<dc:creator>kev</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Big O]]></category>
		<category><![CDATA[Java2D]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[SVG]]></category>
		<category><![CDATA[Wezzle]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/kev/?p=112</guid>
		<description><![CDATA[With the abundance of computing power in processors today it is easy to fall into the trap of implementation without thought of performance. It is something I think all programmers do, myself especially.

Computers today have tons of processing power.  Who really cares about algorithm design when the computer can perform millions of calculations per [...]]]></description>
			<content:encoded><![CDATA[<p>With the abundance of computing power in processors today it is easy to fall into the trap of implementation without thought of performance. It is something I think all programmers do, myself especially.</p>
<p><span id="more-112"></span></p>
<p>Computers today have tons of processing power.  Who really cares about algorithm design when the computer can perform millions of calculations per second? This was a common thought of mine until recently. </p>
<p>When I learned big O notation and performance estimation in university I often thought to myself that I would never need to remember it, and it was not really useful for programming today. I was way off. Big O is something I have recently started using again and encourage every programmer to use as a way of estimating algorithm performance.</p>
<p>When we started making Wezzle we initially began designing without real thought of efficiency. Our initial implementation was all about design. However, we were very inexperienced when it came to actually designing good quality code. We quickly ran into some severe performance issues, and the Wezzle design still has some major flaws that really aren&#8217;t worth fixing at this point. One thing I can say with certainty is that my understanding of good design has improved by a drastic amount from this experience.</p>
<p>Our initial game design included the use of <a href="http://www.w3.org/Graphics/SVG/">Scalable Vector Graphics (SVG)</a>. We chose to use SVG because we liked the way they looked and the fact that window resizing was a non-issue. However, we failed to take into account how performance critical games truly are, and how incredibly slow SVG graphics are. </p>
<p>Our initial game was extremely choppy and virtually unplayable on old machines. We spent a lot of time trying to figure out why that was and how to boost performance. Eventually we decided to bitmap our graphics and noticed a SUBSTANTIAL increase in playability and performance. </p>
<p>I was pretty shocked by this entire scenario. First, I had always assumed that performance today was a non-issue, and second, I never thought that the type of graphics we used would make such a huge difference in terms of performance. </p>
<p>SVG wasn&#8217;t even close to all of our performance issues with early versions of Wezzle. We had a bunch of calls that were needlessly being made every game tick (we still find this problem with some of our code) as well as various other design flaws that affected overall performance. We had an inefficient algorithm to refactor the board and just in general a lot of areas that could be streamlined. We also had a bunch of performance issues with label generation (which we fixed with a cacheing scheme) and some troubles with <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/ref/PhantomReference.html">phantom references</a> (yuck!). We made extensive use of the built in <a href="http://en.wikipedia.org/wiki/Profiler_(computer_science)">profiler</a> in <a href="http://www.netbeans.org/">netbeans</a> during our first refactoring phase.</p>
<p>Since then Wezzle has gone through countless refactorings and countless optimizations. Another huge increase in performance came when we switched from <a href="http://java.sun.com/products/java-media/2D/index.jsp/">Java2D</a> to <a href="http://www.lwjgl.org/">OpenGL (lwjgl)</a>. Perhaps in a later post I will talk about some of the various optimizations we have employed in Wezzle, but that is beyond the scope of this article.</p>
<p>Even with all the performance issues in Wezzle, I thought to myself that games are kind of a unique situation that is very performance critical and lots of calculations are happening, maybe performance is only relevant to gaming.</p>
<p>Again I was wrong. As anyone who does any web development can attest to, large database operations can be TERRIBLY inefficient. I am currently under contract developing some code that loads a large number of records into a grid structure. I am limited by the fact that this grid is only usable to us by passing it XML on the client side, as well as various restrictions about look and feel that were dictated by my client. Perhaps in a future post I talk about the multitude problems with the grid I am using, and there are many, but that would take way too much space to type in this post. </p>
<p>Anyway, my first implementation which contained some optimizations took around 15 minutes to update 500 records from the grid. The records were huge and data validation was being done on every individual cell and since the grid had to be submit as a whole (as an XML string) I was parsing and updating the entire grid. </p>
<p>This was not acceptable and also mildly surprising. Going through my code I have optimized even more. I implemented a dirty cell scheme, marking cells which have been edited as dirty so that on the submit I can only validate cells which have been marked as dirty. This assumes that all data already in the database is valid (not always the case!). I validate pasted in values at paste-time and that&#8217;s it (once they pass validation, they are considered good from the client side). Finally, I mark records which have been modified as dirty and even though I still have to submit the entire grid, I now only parse the records that have been marked and skip the rest. This has vastly improved update performance on the average case. It has improved updates in the worst case as well, but not by quite so much, a full submit still takes some time.</p>
<p>I&#8217;m going to end this by saying that while yes, performance is an issue, it is also sometimes a non-issue. First and foremost you should code something for clarity. A clear design that is slightly less efficient is always better than a streamlined but cryptic design. It is a mistake to optimize something that doesn&#8217;t actually need optimization and a big time waster. A popular saying is &#8220;design for clarity first and foremost and use a profiler to determine which areas need to be optimized.&#8221; This idiom does make sense and makes a valid point, but I would instead say to design for clarity and performance in conjunction as performance can often be a surprising bottleneck. </p>
]]></content:encoded>
			<wfw:commentRss>http://couchware.ca/www/kev/2009/03/10/performance-still-matters/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

