<?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>Zombie Turtle</title>
	<atom:link href="http://zombieturtle.co.uk/feed/" rel="self" type="application/rss+xml" />
	<link>http://zombieturtle.co.uk</link>
	<description>Computers, Programming and Other Junk</description>
	<lastBuildDate>Wed, 19 May 2010 22:50:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Erlang Echo Server</title>
		<link>http://zombieturtle.co.uk/2010/05/erlang-echo-server/</link>
		<comments>http://zombieturtle.co.uk/2010/05/erlang-echo-server/#comments</comments>
		<pubDate>Wed, 19 May 2010 22:50:43 +0000</pubDate>
		<dc:creator>ojhp</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[echo]]></category>
		<category><![CDATA[erlang]]></category>
		<category><![CDATA[sockets]]></category>

		<guid isPermaLink="false">http://zombieturtle.co.uk/?p=187</guid>
		<description><![CDATA[In this post, I&#8217;ll be looking at an example of a simple Erlang program: an echo server. An echo server accepts connections from clients and simply responds to any data received with the same data. While the example given here is quite simple, it is able to deal with multiple simultaneous client connections and handles [...]]]></description>
			<content:encoded><![CDATA[<p>In this post, I&#8217;ll be looking at an example of a simple Erlang program: an echo server. An echo server accepts connections from clients and simply responds to any data received with the same data. While the example given here is quite simple, it is able to deal with multiple simultaneous client connections and handles most errors in a sensible way.</p>
<p><span id="more-187"></span></p>
<h3>The Module Header</h3>
<div class="codecolorer-container erlang default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="erlang codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #014ea4;">-</span><span style="color: #5400b3;">module</span><span style="color: #109ab8;">&#40;</span>echo<span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span><br />
<span style="color: #014ea4;">-</span><span style="color: #5400b3;">export</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#91;</span>start<span style="color: #014ea4;">/</span><span style="color: #ff9600;">1</span><span style="color: #6bb810;">,</span> handle_connections<span style="color: #014ea4;">/</span><span style="color: #ff9600;">1</span><span style="color: #6bb810;">,</span><br />
&nbsp; &nbsp; handle_client<span style="color: #014ea4;">/</span><span style="color: #ff9600;">1</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span><br />
<br />
<span style="color: #014ea4;">-</span><span style="color: #5400b3;">define</span><span style="color: #109ab8;">&#40;</span><span style="color: #6941fd;">TCP_OPTIONS</span><span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#91;</span><span style="color: #fa6fff;">binary</span><span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#123;</span>packet<span style="color: #6bb810;">,</span> raw<span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span><br />
&nbsp; &nbsp; <span style="color: #109ab8;">&#123;</span>active<span style="color: #6bb810;">,</span> false<span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#123;</span>reuseaddr<span style="color: #6bb810;">,</span> true<span style="color: #109ab8;">&#125;</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span></div></td></tr></tbody></table></div>
<p>First off I define the module, which is named <code class="codecolorer text default"><span class="text">echo</span></code> and so it should go in the file &#8220;echo.erl&#8221;. Then I export the functions that make up the server. The last line of the header defines a constant called <code class="codecolorer text default"><span class="text">TCP_OPTIONS</span></code> with a value which is a list of options which will be used by the sockets in the server program. This constant may be used in code later as <code class="codecolorer text default"><span class="text">?TCP_OPTIONS</span></code> (note the question mark). The options are as follows:</p>
<dl>
<dt><code class="codecolorer text default"><span class="text">binary</span></code></dt>
<dd>Data received through the sockets should be in the form of a binary object. There&#8217;s no need for me to cover binary terms right now, as they aren&#8217;t really covered in this example.</dd>
<dt><code class="codecolorer text default"><span class="text">{packet, raw}</span></code></dt>
<dd>The data received should just be the raw data. It is possible to have the data delivered with a header specifying how much data was received, but I don&#8217;t want that here.</dd>
<dt><code class="codecolorer text default"><span class="text">{active, false}</span></code></dt>
<dd>Data will only be read when requested by the program. It is possible to have the data delivered to a process in the form of a message, but again that isn&#8217;t what is wanted here.</dd>
<dt><code class="codecolorer text default"><span class="text">{reuseaddr, true}</span></code></dt>
<dd>This makes sure that the listening socket can be bound even if there is already a socket bound there. This prevents errors if the server crashes and is restarted before the original socket becomes properly closed.</dd>
</dl>
<h3>Starting the Server</h3>
<div class="codecolorer-container erlang default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>8<br />9<br />10<br />11<br />12<br />13<br />14<br /></div></td><td><div class="erlang codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff3c00;">start</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Port</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span><br />
&nbsp; <span style="color: #186895;">case</span> <span style="color: #ff4e18;">gen_tcp</span>:<span style="color: #ff3c00;">listen</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Port</span><span style="color: #6bb810;">,</span> ?<span style="color: #6941fd;">TCP_OPTIONS</span><span style="color: #109ab8;">&#41;</span> <span style="color: #186895;">of</span><br />
&nbsp; &nbsp; <span style="color: #109ab8;">&#123;</span>ok<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">LSock</span><span style="color: #109ab8;">&#125;</span> <span style="color: #6bb810;">-&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #fa6fff;">spawn</span><span style="color: #109ab8;">&#40;</span>?<span style="color: #6941fd;">MODULE</span><span style="color: #6bb810;">,</span> handle_connections<span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">LSock</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">;</span><br />
&nbsp; &nbsp; <span style="color: #109ab8;">&#123;</span>error<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Reason</span><span style="color: #109ab8;">&#125;</span> <span style="color: #6bb810;">-&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #ff4e18;">io</span>:<span style="color: #ff3c00;">fwrite</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot;Error: ~p~n&quot;</span><span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">Reason</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><br />
&nbsp; <span style="color: #186895;">end</span><span style="color: #6bb810;">.</span></div></td></tr></tbody></table></div>
<p>This function starts the server. It takes one argument which is the port to listen for connections on. First it calls the function <code class="codecolorer text default"><span class="text">listen</span></code> from the <code class="codecolorer text default"><span class="text">gen_tcp</span></code> library with the port number and the TCP options constant as defined in the header. This attempts to open the listening socket.</p>
<p>If this succeeds, <code class="codecolorer text default"><span class="text">spawn</span></code> is called to create a new process. The three arguments to this function are the module in which the function to run in this new process resides, the name of the function and a list of arguments to the function. In this case the function is called <code class="codecolorer text default"><span class="text">handle_connections</span></code> and it is in the current module (<code class="codecolorer text default"><span class="text">?MODULE</span></code> is a macro which is replaced with the name of the current module). We pass the listening socket opened with <code class="codecolorer text default"><span class="text">gen_tcp:listen</span></code> to the function.</p>
<p>If opening the listening socket fails, we call <code class="codecolorer text default"><span class="text">io:fwrite</span></code> to write an error message containing the reason for the error.</p>
<h3>Handling Connections</h3>
<div class="codecolorer-container erlang default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br /></div></td><td><div class="erlang codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff3c00;">handle_connections</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">LSock</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span><br />
&nbsp; <span style="color: #186895;">case</span> <span style="color: #ff4e18;">gen_tcp</span>:<span style="color: #ff3c00;">accept</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">LSock</span><span style="color: #109ab8;">&#41;</span> <span style="color: #186895;">of</span><br />
&nbsp; &nbsp; <span style="color: #109ab8;">&#123;</span>ok<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Sock</span><span style="color: #109ab8;">&#125;</span> <span style="color: #6bb810;">-&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #fa6fff;">spawn</span><span style="color: #109ab8;">&#40;</span>?<span style="color: #6941fd;">MODULE</span><span style="color: #6bb810;">,</span> handle_client<span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">Sock</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">;</span><br />
&nbsp; &nbsp; <span style="color: #109ab8;">&#123;</span>error<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Reason</span><span style="color: #109ab8;">&#125;</span> <span style="color: #6bb810;">-&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #ff4e18;">io</span>:<span style="color: #ff3c00;">format</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot;Error: ~p~n&quot;</span><span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">Reason</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><br />
&nbsp; <span style="color: #186895;">end</span><span style="color: #6bb810;">,</span><br />
&nbsp; <span style="color: #ff3c00;">handle_connections</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">LSock</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span></div></td></tr></tbody></table></div>
<p>This is the function that is run in a process created by the <code class="codecolorer text default"><span class="text">start</span></code> function. I takes as an argument a listening socket to listen for connections on. This function calls <code class="codecolorer text default"><span class="text">gen_tcp:accept</span></code> to accept a connection from the listening socket. Again if this results in an error we print a message. If it is successful, a new process is started to handle the client that connected. This runs the function <code class="codecolorer text default"><span class="text">handle_client</span></code> in the current module and passes it the client socket as an argument.</p>
<p>Once this function has completed, it calls itself again with the listening socket. As Erlang provides no looping constructs, loops are created by recursive calls. As long as the call to the function is the last line executed in that function, this will not cause stack problems as it would in many languages such as C and its derivatives. This function keeps accepting connections and spawning client handler processes until the process it is running in is killed.</p>
<h3>Handling Clients</h3>
<div class="codecolorer-container erlang default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br /></div></td><td><div class="erlang codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff3c00;">handle_client</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Sock</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span><br />
&nbsp; <span style="color: #186895;">case</span> <span style="color: #ff4e18;">gen_tcp</span>:<span style="color: #ff3c00;">recv</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Sock</span><span style="color: #6bb810;">,</span> <span style="color: #ff9600;">0</span><span style="color: #109ab8;">&#41;</span> <span style="color: #186895;">of</span><br />
&nbsp; &nbsp; <span style="color: #109ab8;">&#123;</span>ok<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Data</span><span style="color: #109ab8;">&#125;</span> <span style="color: #6bb810;">-&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #ff4e18;">gen_tcp</span>:<span style="color: #ff3c00;">send</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Sock</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Data</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #ff3c00;">handle_client</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Sock</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">;</span><br />
&nbsp; &nbsp; <span style="color: #109ab8;">&#123;</span>error<span style="color: #6bb810;">,</span> closed<span style="color: #109ab8;">&#125;</span> <span style="color: #6bb810;">-&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #006600;">ok</span><span style="color: #6bb810;">;</span><br />
&nbsp; &nbsp; <span style="color: #109ab8;">&#123;</span>error<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Reason</span><span style="color: #109ab8;">&#125;</span> <span style="color: #6bb810;">-&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #ff4e18;">io</span>:<span style="color: #ff3c00;">fwrite</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot;Error: ~p~n&quot;</span><span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">Reason</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><br />
&nbsp; <span style="color: #186895;">end</span><span style="color: #6bb810;">.</span></div></td></tr></tbody></table></div>
<p>This is the function spawned whenever a client connects to the server. It first attempts to receive some data from the client socket passed to it with the <code class="codecolorer text default"><span class="text">gen_tcp:recv</span></code> function which takes the socket to receive from and a number of bytes to receive. As we want to receive all available data, we pass 0 which means there is no specified maximum number of bytes to accept.</p>
<p>If successful, the data is sent back to the client (this is what the echo server is designed to do) with the <code class="codecolorer text default"><span class="text">gen_tcp:send</span></code> function. The function then calls itself to create a loop as with the connection handler. Here the recursive call is not the last line of the function, but as none of the rest of the code in the case statement will be run if the data is successfully received, it is the last line executed. There are therefore again no problems with the call stack overflowing.</p>
<p>If the result <code class="codecolorer text default"><span class="text">{error, closed}</span></code> is obtained, this means that the client has closed the connection. This is normal behaviour, so no error message is printed, the function simply exits and the process will stop. Unfortunately, the case statement must evaluate to a value, so it is necessary to give a value here. In this case I have chosen <code class="codecolorer text default"><span class="text">ok</span></code> although any value would do.</p>
<p>The final part of the case statement prints an error message again if there was a problem receiving data from the client. It is assumed that this error is fatal and so the process stops.</p>
<h3>The Complete Program</h3>
<div class="codecolorer-container erlang default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br /></div></td><td><div class="erlang codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #014ea4;">-</span><span style="color: #5400b3;">module</span><span style="color: #109ab8;">&#40;</span>echo<span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span><br />
<span style="color: #014ea4;">-</span><span style="color: #5400b3;">export</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#91;</span>start<span style="color: #014ea4;">/</span><span style="color: #ff9600;">1</span><span style="color: #6bb810;">,</span> handle_connections<span style="color: #014ea4;">/</span><span style="color: #ff9600;">1</span><span style="color: #6bb810;">,</span><br />
&nbsp; &nbsp; handle_client<span style="color: #014ea4;">/</span><span style="color: #ff9600;">1</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span><br />
<br />
<span style="color: #014ea4;">-</span><span style="color: #5400b3;">define</span><span style="color: #109ab8;">&#40;</span><span style="color: #6941fd;">TCP_OPTIONS</span><span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#91;</span><span style="color: #fa6fff;">binary</span><span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#123;</span>packet<span style="color: #6bb810;">,</span> raw<span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span><br />
&nbsp; &nbsp; <span style="color: #109ab8;">&#123;</span>active<span style="color: #6bb810;">,</span> false<span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#123;</span>reuseaddr<span style="color: #6bb810;">,</span> true<span style="color: #109ab8;">&#125;</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span><br />
<br />
<span style="color: #ff3c00;">start</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Port</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span><br />
&nbsp; <span style="color: #186895;">case</span> <span style="color: #ff4e18;">gen_tcp</span>:<span style="color: #ff3c00;">listen</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Port</span><span style="color: #6bb810;">,</span> ?<span style="color: #6941fd;">TCP_OPTIONS</span><span style="color: #109ab8;">&#41;</span> <span style="color: #186895;">of</span><br />
&nbsp; &nbsp; <span style="color: #109ab8;">&#123;</span>ok<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">LSock</span><span style="color: #109ab8;">&#125;</span> <span style="color: #6bb810;">-&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #fa6fff;">spawn</span><span style="color: #109ab8;">&#40;</span>?<span style="color: #6941fd;">MODULE</span><span style="color: #6bb810;">,</span> handle_connections<span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">LSock</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">;</span><br />
&nbsp; &nbsp; <span style="color: #109ab8;">&#123;</span>error<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Reason</span><span style="color: #109ab8;">&#125;</span> <span style="color: #6bb810;">-&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #ff4e18;">io</span>:<span style="color: #ff3c00;">fwrite</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot;Error: ~p~n&quot;</span><span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">Reason</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><br />
&nbsp; <span style="color: #186895;">end</span><span style="color: #6bb810;">.</span><br />
<br />
<span style="color: #ff3c00;">handle_connections</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">LSock</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span><br />
&nbsp; <span style="color: #186895;">case</span> <span style="color: #ff4e18;">gen_tcp</span>:<span style="color: #ff3c00;">accept</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">LSock</span><span style="color: #109ab8;">&#41;</span> <span style="color: #186895;">of</span><br />
&nbsp; &nbsp; <span style="color: #109ab8;">&#123;</span>ok<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Sock</span><span style="color: #109ab8;">&#125;</span> <span style="color: #6bb810;">-&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #fa6fff;">spawn</span><span style="color: #109ab8;">&#40;</span>?<span style="color: #6941fd;">MODULE</span><span style="color: #6bb810;">,</span> handle_client<span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">Sock</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">;</span><br />
&nbsp; &nbsp; <span style="color: #109ab8;">&#123;</span>error<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Reason</span><span style="color: #109ab8;">&#125;</span> <span style="color: #6bb810;">-&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #ff4e18;">io</span>:<span style="color: #ff3c00;">format</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot;Error: ~p~n&quot;</span><span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">Reason</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><br />
&nbsp; <span style="color: #186895;">end</span><span style="color: #6bb810;">,</span><br />
&nbsp; <span style="color: #ff3c00;">handle_connections</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">LSock</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span><br />
<br />
<span style="color: #ff3c00;">handle_client</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Sock</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span><br />
&nbsp; <span style="color: #186895;">case</span> <span style="color: #ff4e18;">gen_tcp</span>:<span style="color: #ff3c00;">recv</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Sock</span><span style="color: #6bb810;">,</span> <span style="color: #ff9600;">0</span><span style="color: #109ab8;">&#41;</span> <span style="color: #186895;">of</span><br />
&nbsp; &nbsp; <span style="color: #109ab8;">&#123;</span>ok<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Data</span><span style="color: #109ab8;">&#125;</span> <span style="color: #6bb810;">-&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #ff4e18;">gen_tcp</span>:<span style="color: #ff3c00;">send</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Sock</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Data</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #ff3c00;">handle_client</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Sock</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">;</span><br />
&nbsp; &nbsp; <span style="color: #109ab8;">&#123;</span>error<span style="color: #6bb810;">,</span> closed<span style="color: #109ab8;">&#125;</span> <span style="color: #6bb810;">-&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #006600;">ok</span><span style="color: #6bb810;">;</span><br />
&nbsp; &nbsp; <span style="color: #109ab8;">&#123;</span>error<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Reason</span><span style="color: #109ab8;">&#125;</span> <span style="color: #6bb810;">-&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #ff4e18;">io</span>:<span style="color: #ff3c00;">fwrite</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot;Error: ~p~n&quot;</span><span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">Reason</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><br />
&nbsp; <span style="color: #186895;">end</span><span style="color: #6bb810;">.</span></div></td></tr></tbody></table></div>
<p>Here is the complete code for the program, put this in a file called &#8220;echo.erl&#8221;. To run it start an Erlang shell in the directory where you have saved the file and run the following two commands to get it started:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">c(echo).<br />
echo:start(1234).</div></div>
<p>This will start the server listening for connections on port 1234. You can then connect to it via telnet or something similar, anything you send to the server should be echoed back to you. There is no way of stopping the server appart from killing all the processes that are handling connections and clients so it&#8217;s probably easier to just to close Erlang down.</p>
]]></content:encoded>
			<wfw:commentRss>http://zombieturtle.co.uk/2010/05/erlang-echo-server/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Starting Erlang</title>
		<link>http://zombieturtle.co.uk/2010/05/starting-erlang/</link>
		<comments>http://zombieturtle.co.uk/2010/05/starting-erlang/#comments</comments>
		<pubDate>Sun, 16 May 2010 15:47:10 +0000</pubDate>
		<dc:creator>ojhp</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[erlang]]></category>
		<category><![CDATA[hello world]]></category>

		<guid isPermaLink="false">http://zombieturtle.co.uk/?p=175</guid>
		<description><![CDATA[Since the last few posts I&#8217;ve moved on from Haskell and am now looking at Erlang, another functional language developed by Ericsson and released as open source in 1998. I&#8217;ll start by giving and explaining the code for a simple &#8220;Hello World!&#8221; program. 12345-module&#40;hello&#41;. -export&#40;&#91;hello/0&#93;&#41;. hello&#40;&#41; -&#62; &#160; &#160; io:format&#40;&#34;Hello World!\n&#34;&#41;. This is a basic [...]]]></description>
			<content:encoded><![CDATA[<p>Since the last few posts I&#8217;ve moved on from Haskell and am now looking at Erlang, another functional language developed by Ericsson and released as open source in 1998. I&#8217;ll start by giving and explaining the code for a simple &#8220;Hello World!&#8221; program.</p>
<p><span id="more-175"></span></p>
<div class="codecolorer-container erlang default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="erlang codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #014ea4;">-</span><span style="color: #5400b3;">module</span><span style="color: #109ab8;">&#40;</span>hello<span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span><br />
<span style="color: #014ea4;">-</span><span style="color: #5400b3;">export</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#91;</span>hello<span style="color: #014ea4;">/</span><span style="color: #ff9600;">0</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span><br />
<br />
<span style="color: #ff3c00;">hello</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span><br />
&nbsp; &nbsp; <span style="color: #ff4e18;">io</span>:<span style="color: #ff3c00;">format</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot;Hello World!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span></div></td></tr></tbody></table></div>
<p>This is a basic &#8220;Hello World!&#8221; program, don&#8217;t miss the full stops, they&#8217;re very important. The first line defines the module that is provided in this file. The file and module name must be the same, so this would go in the file <tt>hello.erl</tt>. On the next line, the functions that this module provides are given, in this case a single function called hello which takes 0 arguments.</p>
<p>Next the hello function is declared, the code which makes up the function follows the arrow <code class="codecolorer text default"><span class="text">-&gt;</span></code>. This calls the function <code class="codecolorer text default"><span class="text">format</span></code>, from the module <code class="codecolorer text default"><span class="text">io</span></code>, which takes a string and outputs it to the terminal.</p>
<p>To run this code, we need to start erlang in a terminal, compile and load the <code class="codecolorer text default"><span class="text">hello</span></code> module and call the hello function from within it. This should go as follows:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ erl<br />
Erlang R13B03 (erts-5.7.4) [source] [64-bit] [smp:2:2]<br />
[rq:2] [async-threads:0] [hipe] [kernel-poll:false]<br />
<br />
EShell V5.7.4 (abort with ^G)<br />
1&gt; c(hello).<br />
{ok,hello}<br />
2&gt; hello:hello().<br />
Hello World!<br />
ok<br />
3&gt;</div></td></tr></tbody></table></div>
<p>The Erlang shell is started from the command line by running <code class="codecolorer text default"><span class="text">erl</span></code>, then we call the compiler with the built-in function (BIF) <code class="codecolorer text default"><span class="text">c</span></code> with the argument <code class="codecolorer text default"><span class="text">hello</span></code>, which compiles the module and loads the compiled code for use. Assuming this is successfull, the function returns a tuple consisting of two elements. The first is <code class="codecolorer text default"><span class="text">ok</span></code>, which means that the action was successful and the second is the name of the compiled module. This tuple value is printed to the terminal before the shell returns to a prompt.</p>
<p>Finally we call the <code class="codecolorer text default"><span class="text">hello</span></code> function from the module <code class="codecolorer text default"><span class="text">hello</span></code> with no arguments, which prints the line &#8220;Hello World!&#8221; to the screen followed by a newline. This function returns the value <code class="codecolorer text default"><span class="text">ok</span></code> which is then printed before the shell returns to the prompt.</p>
<h3>NOTE: Exiting the Shell</h3>
<p>Getting out of the Erlang shell is slightly more complicated than you might expect. Unlike most shells where a simple Ctrl-D will signal the end of input and get you out of there, you must press Ctrl-C to cause a break in the Erlang virtual machine and then press &#8220;a&#8221; and return to tell it you want to abort execution and quit the program.</p>
]]></content:encoded>
			<wfw:commentRss>http://zombieturtle.co.uk/2010/05/starting-erlang/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fibonacci Numbers</title>
		<link>http://zombieturtle.co.uk/2010/02/fibonacci-numbers/</link>
		<comments>http://zombieturtle.co.uk/2010/02/fibonacci-numbers/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 15:59:42 +0000</pubDate>
		<dc:creator>ojhp</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[fibonacci]]></category>
		<category><![CDATA[haskell]]></category>

		<guid isPermaLink="false">http://zombieturtle.co.uk/?p=124</guid>
		<description><![CDATA[As I said in my last post, I will look at generating Fibonacci numbers in Haskell. I will look at a simple but slow algorithm followed by a much faster version to highlight the importance of algorithm design. First Attempt 1234fib :: Integer -&#62; Integer fib 0 = 1 fib 1 = 1 fib n [...]]]></description>
			<content:encoded><![CDATA[<p>As I said in my last post, I will look at generating Fibonacci numbers in Haskell. I will look at a simple but slow algorithm followed by a much faster version to highlight the importance of algorithm design.</p>
<p><span id="more-124"></span></p>
<h3>First Attempt</h3>
<div class="codecolorer-container haskell default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="haskell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">fib <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Integer</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Integer</span><br />
fib <span style="color: red;">0</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">1</span><br />
fib <span style="color: red;">1</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">1</span><br />
fib n <span style="color: #339933; font-weight: bold;">=</span> fib <span style="color: green;">&#40;</span>n <span style="color: #339933; font-weight: bold;">-</span> <span style="color: red;">1</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">+</span> fib <span style="color: green;">&#40;</span>n <span style="color: #339933; font-weight: bold;">-</span> <span style="color: red;">2</span><span style="color: green;">&#41;</span></div></td></tr></tbody></table></div>
<p>This takes the basic definition of Fibonacci numbers and  translates it into code. The first two numbers are both 1 and any following numbers are equal to the sum of the previous two numbers. As this function calls itself recursively twice, it is very slow and ends up preforming the same calculations twice if a number greater than three is passed as an argument.</p>
<h3>An Improved Algorithm</h3>
<div class="codecolorer-container haskell default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="haskell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">fib <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Integer</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Integer</span><br />
fib <span style="color: red;">0</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">1</span><br />
fib <span style="color: red;">1</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">1</span><br />
fib n <span style="color: #339933; font-weight: bold;">=</span> fib2 <span style="color: red;">1</span> <span style="color: red;">1</span> n<br />
<br />
fib2 <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Integer</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Integer</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Integer</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Integer</span><br />
fib2 n n1 <span style="color: red;">2</span> <span style="color: #339933; font-weight: bold;">=</span> n <span style="color: #339933; font-weight: bold;">+</span> n1<br />
fib2 n n1 i <span style="color: #339933; font-weight: bold;">=</span> fib2 <span style="color: green;">&#40;</span>n <span style="color: #339933; font-weight: bold;">+</span> n1<span style="color: green;">&#41;</span> n <span style="color: green;">&#40;</span>i <span style="color: #339933; font-weight: bold;">-</span> <span style="color: red;">1</span><span style="color: green;">&#41;</span></div></td></tr></tbody></table></div>
<p>This function uses another &#8220;fib2&#8243; to calculate numbers past the first two. This new function calls itself recursively, but keeps track of the last two numbers in the sequence in the first two arguments. This means that it only needs to perform a single call to itself, drastically reducing the time taken to calculate the values. The final argument simply counts down to let the function know when it has finished and to return the sum of the last two values.</p>
<h3>Speed Comparison</h3>
<p>I timed the running of both algorithms on my laptop (not the most powerful computer, but I think it still shows the speed difference). With the first algorithm, calculation of the 30th Fibonacci number took 16 seconds. With the second, it took only half a second. In fact in the time it took the first algorithm to find the 30th number, the second algorithm could compute the 120,000th number in the sequence.</p>
<p>These comparisons were done without compiling the code, so there would be some interpreter overheads involved, but as shown, these overheads had only a small impact on the overall run time.</p>
]]></content:encoded>
			<wfw:commentRss>http://zombieturtle.co.uk/2010/02/fibonacci-numbers/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>First Steps in Haskell</title>
		<link>http://zombieturtle.co.uk/2010/01/first-steps-in-haskell/</link>
		<comments>http://zombieturtle.co.uk/2010/01/first-steps-in-haskell/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 00:33:33 +0000</pubDate>
		<dc:creator>ojhp</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[hello world]]></category>

		<guid isPermaLink="false">http://ojhp.co.uk/?p=15</guid>
		<description><![CDATA[Recently I have decided to teach myself the Haskell programming langauge. My first step was to grab a copy of the O&#8217;Reilly book on the langauge, which while slightly out of date, I can highly recommend. After digesting the earlier chapters, I was ready to write some simple programs. The first being the obligatory &#8220;Hello [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I have decided to teach myself the Haskell programming langauge. My first step was to grab a copy of the <a title="&quot;Real World Haskell&quot; on Amazon.co.uk" href="http://www.amazon.co.uk/Real-World-Haskell-Code-Believe/dp/0596514980/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1264118927&amp;sr=8-1">O&#8217;Reilly book</a> on the langauge, which while slightly out of date, I can highly recommend. After digesting the earlier chapters, I was ready to write some simple programs. The first being the obligatory &#8220;Hello World!&#8221; program, which is incredibly simple.</p>
<p><span id="more-111"></span></p>
<div class="codecolorer-container haskell default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="haskell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #06c; font-weight: bold;">module</span> Main <span style="color: #06c; font-weight: bold;">where</span><br />
<br />
main <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">IO</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span><br />
main <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">putStrLn</span> <span style="background-color: #3cb371;">&quot;Hello World!&quot;</span></div></td></tr></tbody></table></div>
<p>The first line says that this is the <code class="codecolorer text default"><span class="text">Main</span></code> module of the program being compiled. The next line <code class="codecolorer haskell default"><span class="haskell">main <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">IO</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span></span></code> declares a <code class="codecolorer text default"><span class="text">main</span></code> function which performs IO operations and returns nothing, shown by the symbol <code class="codecolorer text default"><span class="text">()</span></code>. The function <code class="codecolorer text default"><span class="text">main</span></code> in the module <code class="codecolorer text default"><span class="text">Main</span></code> is where the program execution begins.</p>
<p>These first two lines are actually omissable as the compiler assumes that unless specified, we are in the <code class="codecolorer text default"><span class="text">Main</span></code> module and it can guess the types of functions from their implementations.</p>
<p>The final line specifies what the <code class="codecolorer text default"><span class="text">main</span></code> function actually does. It performs the action <code class="codecolorer text default"><span class="text">putStrLn</span></code> (print a string and then a newline) on the string &#8220;Hello World!&#8221;.</p>
<p>To compile and run this program using <a href="http://www.haskell.org/ghc/">GHC</a> (Glasgow Haskell Compiler) simply put the code in a file called &#8220;hello.hs&#8221; and type the following at the command line:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ ghc -o hello hello.hs<br />
$ ./hello<br />
Hello World!<br />
$</div></div>
<p>The next thing I usually write in a new language is a program to calculate numbers in the Fibonacci sequence, for which I have an easily understood program and a much faster algorithm which I will write about next time.</p>
]]></content:encoded>
			<wfw:commentRss>http://zombieturtle.co.uk/2010/01/first-steps-in-haskell/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

