<?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>Daniel15&#039;s Blog &#187; University</title>
	<atom:link href="http://dan.cx/blog/category/university/feed/" rel="self" type="application/rss+xml" />
	<link>http://dan.cx/blog</link>
	<description>Blog of Daniel, a slightly awesome 19-year-old web developer from Melbourne, Australia</description>
	<lastBuildDate>Sun, 15 Aug 2010 02:22:19 +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>Daniel15&#8242;s Introduction to Object-Oriented Programming</title>
		<link>http://dan.cx/blog/2009/04/introduction-to-object-oriented-programming/</link>
		<comments>http://dan.cx/blog/2009/04/introduction-to-object-oriented-programming/#comments</comments>
		<pubDate>Sat, 11 Apr 2009 05:57:44 +0000</pubDate>
		<dc:creator>Daniel15</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[HIT2302]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Add new tag]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[object orientation]]></category>
		<category><![CDATA[object oriented]]></category>

		<guid isPermaLink="false">http://d15.biz/blog/?p=162</guid>
		<description><![CDATA[Well, back to posting coding-related blog posts, for now anyways . Seeing as a lot of people seem to be confused by Object Oriented Programming, I thought I&#8217;d post a quick (or maybe not so quick) post about what OOP is, the main features, and how it can benefit you. This is paraphrased from an [...]]]></description>
			<content:encoded><![CDATA[<p>Well, back to posting coding-related blog posts, for now anyways <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> . Seeing as a lot of people seem to be confused by Object Oriented Programming, I thought I&#8217;d post a quick (or maybe not so quick) post about what OOP is, the main features, and how it can benefit you. This is paraphrased from an assignment I had on OOP last semester at university. I use C# code examples throughout this, but the concepts are very similar in other languages. Note that in this post, I assume you know the basics of programming, and just want to learn more about object orientation.</p>
<p>Now, let&#8217;s begin looking at what OOP actually means. At its core, the Object Oriented paradigm consists of <strong>classes</strong> and <strong>objects</strong>. A class is a “thing” or entity that has a purpose, and an <strong>object</strong> is an instance of this entity. For example, a Car would be a class, and <strong>my car</strong> would be an object (instance of the Car class).<br />
<span id="more-162"></span></p>
<h3>Classes in C#</h3>
<p>Before we can cover any of the OO principles with code examples, we need to go through some very simple code. A class in C# is made using the class keyword:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Car
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// ...</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The &#8220;<em>public</em>&#8221; is an <strong>access modifier</strong>. The available access modifiers for classes in C# are:</p>
<ul>
<li><strong>public</strong> – No restrictions on accessibility of the class. It can be used from anywhere.</li>
<li><strong>private</strong> – Can only be accessed by code in the same class.</li>
<li><strong>protected</strong> – Can only be accessed by code in the same class, or in a derived class (see <strong>inheritance</strong>, below).</li>
<li><strong>internal</strong> – Can be accessed by any code in the same assembly (basically, the same file), but not from another assembly.</li>
</ul>
<p>Methods in the class are specified like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">virtual</span> <span style="color: #0600FF;">void</span> StartCar<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;">// ...</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Like above, <em>public</em> is an access modifier. The <strong>virtual</strong> keyword means that this method can be overridden in child classes  (this is explained under “inheritance” below). “void” is the return type. A method that returns void basically returns nothing (the same as void in languages like C#, or procedures [as opposed to functions] in Pascal and Visual Basic). Some common return types include string, int, uint (unsigned int), long, ulong, float (Single-precision floating point number, 4 bytes) and double (Double-precision floating point number, 8 bytes)</p>
<h2>Core OO principles</h2>
<p>With all that said, let&#8217;s jump into the core OOP concepts. There are three “pillars” of OOP; these are the core principles of Object Oriented Programming. These are Encapsulation, Inheritance and Polymorphism. In addition to these, there are several other core ideas.</p>
<h3>Abstraction</h3>
<p>An abstraction is basically an idea, something that we think about to simplify things. In our car example (which will be used throughout this portfolio), a <strong>Car</strong> is an abstraction.</p>
<p>Encapsulation, Inheritance and Polymorphism (the pillars of OOP) are all abstractions.</p>
<h3>Object Composition</h3>
<p>Object composition is basically a way to combine simpler objects into larger, more complex ones. This is known as a “<strong>has-a</strong>” relationship. For example, a <strong>Car</strong> object might contain an Engine, Wheels, etc. We don&#8217;t need to know how all these other smaller objects work, but only how to interface with them (this is directly related to Encapsulation, see below).</p>
<h3>Encapsulation</h3>
<p>Encapsulation is the “hiding” of internal details of a class. This makes the class “friendlier”, and hides all its internals from the user. For example, a “car” class in C# could look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Car
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">virtual</span> <span style="color: #0600FF;">void</span> StartCar<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;">// Do whatever the car needs to do to start</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// More class members here</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>We can use this class without knowing exactly how it starts the car, just that it starts it:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Car myCar <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Car<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
myCar.<span style="color: #0000FF;">StartCar</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>This also ensures that code outside the class can&#8217;t access data that it shouldn&#8217;t be allowed to touch. Allowing free access to all <strong>fields</strong> (values that the object “knows”) could lead to data corruption, as the object could be in an unknown state. For example, the car could have a field that says how much petrol is left in its petrol tank. We want the car to know how much petrol is left, but we don&#8217;t want other classes to be able to just change this value. In C#, we use private variables for this purpose:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Car
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #FF0000;">int</span> _petrol<span style="color: #008000;">;</span>
	<span style="color: #008080; font-style: italic;">// ...</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>We could then have a <strong>property</strong> to access this, and choose to only have a getter (no setter) for it, meaning that it can only be read:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Car
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #FF0000;">int</span> _petrol<span style="color: #008000;">;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Petrol
	<span style="color: #000000;">&#123;</span>
			get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _petrol<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #008080; font-style: italic;">// ...</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<h3>Inheritance</h3>
<p>Inheritance basically allows us to build new classes which are based on existing classes. The new (lower-level) classes inherit all the functionality of those above it, and can also add new functionality. For example, we cam make a <strong>SportsCar</strong> class that inherits from Car:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> SportsCar <span style="color: #008000;">:</span> Car
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// ...</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The SportsCar class will automatically have all the members that Car had (the petrol field, StartCar method, etc.), as well as any new members we define. To override a method in the Car class, we can use the <strong>override</strong> keyword. As long as the function is defined with “virtual”, this will work fine:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> StartCar<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;">// ...</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>In fact, we could take this abstraction even further, and have a <strong>Vehicle</strong> class which the Car inherits from. Then we could have other vehicles (e.g. Bike, Bus, etc.) which all have common elements, and can be treated in the same way (polymorphism, described below)</p>
<h3>Polymorphism</h3>
<p>Polymorphism is the ability to access multiple different types of objects using the same methods. For instance, above, we defined SportsCar as a <strong>derived class</strong> of Car. We can treat SportsCar instances (objects) in the same way as we treat Cars, because the compiler knows that a SportsCar <strong>is a</strong> car:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Car myCar <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Car<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Car mySportsCar <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SportsCar<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
myCar.<span style="color: #0000FF;">StartCar</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
mySportsCar.<span style="color: #0000FF;">StartCar</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>The two types of cars might start very differently, but they&#8217;re both called using the same StartCar method.</p>
<h2>More OO stuffs</h2>
<h3>Constructors</h3>
<p>A constructor is a function that&#8217;s called when an instance of the class is created. They&#8217;re usually used to set parameters at creation time. The constructor is a method with the same name as the class. For example, in our <strong>Car</strong> class, we could have something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Car
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #FF0000;">int</span> _petrol<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> Car<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> petrol<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		_petrol <span style="color: #008000;">=</span> petrol<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This means that when the Car class is created, we&#8217;d need to pass it one argument (not 0, not more than one).</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Car myCar <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Car<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">100</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>We can give a class more than one constructor. In this case, each constructor is differentiated by the number of parameters. If the class is created with one parameter, the constructor with one parameter is called. Same with if the class is created with two parameters, the constructor with two parameters is called.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Car
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">const</span> <span style="color: #FF0000;">int</span> DEFAULT_PETROL <span style="color: #008000;">=</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #FF0000;">int</span> _petrol<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// Default constructor</span>
	<span style="color: #0600FF;">public</span> Car<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;">// They didn't pass a parameter, so assume defaults.</span>
		_petrol <span style="color: #008000;">=</span> DEFAULT_PETROL<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> Car<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> petrol<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		_petrol <span style="color: #008000;">=</span> petrol<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>An example of using these constructors:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// This will create a car, calling the default constructor.</span>
Car myCar <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Car<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// And this uses the constructor with one argument.</span>
Car myOtherCar <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Car<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">50</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<h3>Interfaces</h3>
<p>Interfaces are another core part of OOP in C#. An interface expresses a behaviour that a class may choose to implement. Unlike a derived class, any class may have as many interfaces as you like. For example, we could define a “IExplodable” interface:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">interface</span> IExplodable
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">void</span> Explode<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>This defines an IExplodable interface, with a single method (Explode). Interfaces are always abstract; we can&#8217;t create a direct instance of them. This means that we don&#8217;t use the interface directly, instead we have classes that use the interfaces. For example, we could make our SportsCar IExplodable:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">class</span> SportsCar <span style="color: #008000;">:</span> Car, IExplodable
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// IExplodable implementation</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Explode<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;">// ...</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This makes the SportsCar use the IExplodable interface.</p>
<p>We can treat anything that implements IExplodable in the same way (this is Polymorphism at work). For example, a function like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> ExplodeIt<span style="color: #000000;">&#40;</span>IExplodable thing<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	thing.<span style="color: #0000FF;">Explode</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>We can pass this any object that implements IExplodable:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">SportsCar myCar <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SportsCar<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Bike myBike <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Bike<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Microwave mike <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Microwave<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Potato awesomePotato <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Potato<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
ExplodeIt<span style="color: #000000;">&#40;</span>myCar<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
ExplodeIt<span style="color: #000000;">&#40;</span>myBike<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
ExplodeIt<span style="color: #000000;">&#40;</span>mike<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
ExplodeIt<span style="color: #000000;">&#40;</span>awesomePotato<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<h2>Object design</h2>
<p>Now that I&#8217;ve covered all the basics of object orientation, I thought I&#8217;d quickly mention some things about object design. There are several things that contribute to a good design:</p>
<ul>
<li>Don&#8217;t put all the functionality in the one massive class or method (“centralised”). Instead, spread the functionality across multiple classes and multiple methods. This eases development, makes testing easier (see unit testing, above), and also makes the code more reusable.</li>
<li>Excessive coupling is not good, because it prevents the reuse of objects in other projects. Coupling refers to the degree to which each object relies on one of the other objects. If the objects all rely on each other too much, then changes in one will require a whole heap of changes in others, and they&#8217;re not as reusable.</li>
<li>High cohesion is good. Cohesion refers to how strongly-related or focused the responsibilities of a single class are. If all the methods of the class tend to be similar, the class is said to have high cohesion. High cohesion leads to more readable code, and the likelihood of code reuse inceases. As an example, in the Car class, the methods (eg. StartCar, Drive, etc) all do something to the cat.</li>
</ul>
<h2>Culture</h2>
<p>Talking about programming culture usually involves discussion of coding standards. Coding standards are important because we need to stay consistent throughout our code, so other developers are able to understand it. The main standards that are followed in C# (including those <a href="http://mercury.it.swin.edu.au/swinbrain/index.php/Swinburne_.NET_Coding_Standard">followed at Swinburne</a>) include:</p>
<ul>
<li>Use an underscore at the start of private members of a class (e.g. _petrol in the examples above)</li>
<li>Use camel casing for local variables and parameters. Camel Case is basically words stuck together, with each word except the first starting with a capital letter (e.g. myCar)</li>
<li>Use pascal casing (like camel case, except with the first letter capitalised as well) for the names of Classes and Methods (e.g. Car, SportsCar)</li>
<li>Use an I to prefix the names of interfaces (e.g. IExplodable)</li>
<li>File names should be the same as the class name (e.g. Car.cs, SportsCar.cs)</li>
<li>Use meaningful descriptive words for the names of variables, classes, methods, objects, etc. The names should be useful so we know what the class or method is without looking through all the code.</li>
</ul>
<p>Well, that&#8217;s all for now. Hope this has helped someone <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>&mdash; Daniel</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.cx/blog/2009/04/introduction-to-object-oriented-programming/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>So, haven&#8217;t blogged in a while</title>
		<link>http://dan.cx/blog/2009/02/so-havent-blogged-in-a-while/</link>
		<comments>http://dan.cx/blog/2009/02/so-havent-blogged-in-a-while/#comments</comments>
		<pubDate>Fri, 27 Feb 2009 14:33:33 +0000</pubDate>
		<dc:creator>Daniel15</dc:creator>
				<category><![CDATA[Relationships]]></category>
		<category><![CDATA[University]]></category>
		<category><![CDATA[nothing better to post]]></category>
		<category><![CDATA[psd]]></category>
		<category><![CDATA[swinburne]]></category>
		<category><![CDATA[valentines]]></category>
		<category><![CDATA[valentines day]]></category>

		<guid isPermaLink="false">http://d15.biz/blog/?p=133</guid>
		<description><![CDATA[So, umm, yeah, I don&#8217;t blog often. You&#8217;ve probably noticed. Seriously, look at the &#8220;Archives&#8221; widget on the right (or left, depending on what stylesheet you&#8217;ve chosen for this site) and you&#8217;ll see there&#8217;s basically nothing since 2007. I really just don&#8217;t have much to write, haha. I eventually want to write a page about [...]]]></description>
			<content:encoded><![CDATA[<p>So, umm, yeah, I don&#8217;t blog often. You&#8217;ve probably noticed. Seriously, look at the &#8220;Archives&#8221; widget on the right (or left, depending on what stylesheet you&#8217;ve chosen for this site) and you&#8217;ll see there&#8217;s basically nothing since 2007. I really just don&#8217;t have much to write, haha. I eventually want to write a page about me for the homepage here, but I guess my MySpace and Facebook profiles are serving that purpose right now. In any case, my <a href="http://wordsaboutsomething.com/">sister has a blog</a>, which you should all go read. Unlike me, she has a talent for writing, and I promise you that her blog is a lot more exciting than mine. Go subscribe to her too. And send her lots of spam (well, okay, maybe not the spam, but do all the other things. <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />  ). Also, check out my friend <a href="http://www.benstoneonline.com/">Ben</a>&#8216;s new blog &#8220;<a href="http://epictoast.com/">EpicToast.com</a>.&#8221;</p>
<p>While I&#8217;m on the topic of blogs (including this one), it&#8217;s interesting thing to note, my &#8220;<a href="http://d15.biz/blog/2007/10/37-things-a-girl-probably-doesnt-know-about-a-guy/">37 things a girl probably doesn’t know about a guy</a>&#8221; blog post is still by far the most popular/accessed. And it wasn&#8217;t even mine, it was just copied and pasted from a MySpace bulletin. Maybe one day I&#8217;ll write something similar, with my own thoughts on it. I guess. Speaking of random stuff that gets passed around, I keep getting tagged in those &#8220;12/20/38/50/100/whatever Things About Me&#8221; notes on Facebook, haven&#8217;t done any yet though. I suppose I might do one of those one day. It&#8217;s hard to think of that many things about myself, though. And I also try to avoid those chain things anyways. But one day someone will be like &#8220;OMG DANIEL YOU HAVEN&#8217;T DONE THOSE ANNOYING CHAIN THINGS, I HATE YOU LALALALALALA RAWRRRRRR&#8221; and I&#8217;ll be like &#8220;OH FINE &gt;_&lt;&#8221; and do it. <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<p>So anyways, what&#8217;s been up with me, apart from avoiding chain &#8220;random stuff about me&#8221; Facebook forwards?? Well, Valentines Day came and went&#8230; I bought my amazing friend Ciera some flowers and a valentines day teddy&#8230; She loves them both <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> . I wanted to do something special, seeing as this year&#8217;s the first year I&#8217;ve considered buying something for someone <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> . Apart from that, I haven&#8217;t been doing all that much. Over the last few weeks, I worked on a few programming projects, then got bored of that and stopped working on them xD. Nothing much apart from that, just the usual chatting on Windows Live Messenger, and browsing Digg, news.com.au, and some other sites. </p>
<p>I go back to university on Monday&#8230; Second year of <a href="http://courses.swinburne.edu.au/Courses/ViewCourse.aspx?id=25579">Professional Software Development at Swinburne University</a>, should be fun <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . No usability this semester! Although I reeeeeeeeeeally hope that we don&#8217;t have a subject that&#8217;s as bad as Usability was, <a href="http://courses.swinburne.edu.au/Subjects/ViewSubject.aspx?mi=300&#038;id=4763">HIT2308 Software Development Practices</a> looks like it&#8217;ll be just as bad as Usability :&#8217;(. And I have that THIS SEMESTER. We&#8217;ll have to see if it&#8217;s bad. I blame you (yes, whoever&#8217;s reading this) if it is. You&#8217;re evil <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>For now, I&#8217;ll leave you with this video clip by <a href="http://www.youtube.com/user/GoRemy">GoRemy</a>:<br />
<object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/ItTotBfeLT4&#038;hl=en&#038;fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/ItTotBfeLT4&#038;hl=en&#038;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object><br />
&lt;3 GoRemy</p>
<p>And here is a remix of &#8220;Break Up&#8221; by Kim Sozzi that I started at the start of January:<br />
<object width="400" height="110"><param name="movie" value="http://media.imeem.com/m/c9LHdKGi3C/aus=false/"></param><param name="wmode" value="transparent"></param><embed src="http://media.imeem.com/m/c9LHdKGi3C/aus=false/" type="application/x-shockwave-flash" width="400" height="110" wmode="transparent"></embed></object><br />
<a href="http://stuff.d15.biz/music/Kim%20Sozzi%20-%20Break%20Up%20(Daniel15%20Remix,%202nd%20Iteration).mp3">Here&#8217;s the MP3</a>, if you want it (not that you would, it&#8217;s not too good <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> )</p>
<p>Well, that&#8217;s all for now. Have fun browsing your internets.<br />
&mdash; Daniel</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.cx/blog/2009/02/so-havent-blogged-in-a-while/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Tram line go-karts, university exams, and more</title>
		<link>http://dan.cx/blog/2008/12/tram-line-go-karts-university-exams-and-more/</link>
		<comments>http://dan.cx/blog/2008/12/tram-line-go-karts-university-exams-and-more/#comments</comments>
		<pubDate>Thu, 04 Dec 2008 14:16:08 +0000</pubDate>
		<dc:creator>Daniel15</dc:creator>
				<category><![CDATA[Melbourne]]></category>
		<category><![CDATA[Relationships]]></category>
		<category><![CDATA[University]]></category>
		<category><![CDATA[go-kart]]></category>
		<category><![CDATA[irl]]></category>
		<category><![CDATA[real life]]></category>
		<category><![CDATA[tram]]></category>

		<guid isPermaLink="false">http://d15.biz/blog/?p=73</guid>
		<description><![CDATA[So, I haven&#8217;t blogged for a while&#8230; Mainly due to real-life getting in the way. Yeah, real life gets like that sometimes, it&#8217;s rather annoying . That, and the fact that I&#8217;m lazy &#8211; I won&#8217;t really do anything unless I&#8217;m forced to (or force myself to). Well actually, most people are like this, it&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>So, I haven&#8217;t blogged for a while&#8230; Mainly due to real-life getting in the way. Yeah, real life gets like that sometimes, it&#8217;s rather annoying <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />  . That, and the fact that I&#8217;m lazy &#8211; I won&#8217;t really do anything unless I&#8217;m forced to (or force myself to). Well actually, most people are like this, it&#8217;s just very few seem to admit it. And realisation is the first step to admitting you have a problem, right? <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />  . And I haven&#8217;t really had anything to blog about either. Aaaand this blog has approximately 0 readers. I don&#8217;t think anyone is even reading what I&#8217;m writing right now. If you&#8217;re one of the 0 readers, then HELLOOOOOOOO TO YOOOOOOUUUUUU!!! I have a cookie for you, come and get it before I eat it.</p>
<p>Well anyways, enough of my stupid rambling&#8230; my first year of University is finally over, and geez did the time pass quickly <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_neutral.gif' alt=':|' class='wp-smiley' />  !  It was generally enjoyable, way way more so than high school was. University life is completely different to high school, in a sense it&#8217;s a lot more relaxed. Some people take advantage of this and try to breeze through Uni without studying&#8230; And end up dropping out <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> .  I tried concentrating in class (moreso than last year at high school), but in the end I just ended up not paying attention at all, and rushing all my last assignments (isn&#8217;t this the case with everyone?). Results day is <strong>13th December 2008</strong>, so I guess I&#8217;ll be worrying about my results until then. There&#8217;s one&#8230; no, wait, two things I really hated though:</p>
<h3>1. Exams!</h3>
<p>Okay, sure, everyone hates exams. Me moreso than most I reckon. I seriously HATE HATE HATE exams. On a scale from 1 to OMG I HATE THIS, it&#8217;d be on the very end. Seriously. They drive me crazy. Crazier than I normally am, and that&#8217;s a LOT. <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' />  I seem to often forget everything and have a whole heap of stuff rush back to me at the last minute. I hate it, haha. At least these exams aren&#8217;t as bad as the <a href="http://d15.biz/blog/2007/11/02/survivor-english-exam-edition/">Year 12 English Exam</a> (or maybe Usability is that bad&#8230; I&#8217;ll get to that in a bit)<br />
And especially one of my subjects was a subject about website development using HTML. We had to <strong>write</strong> HTML (yes, by hand) in the exam. Seriously, what is that testing? Most (hell, <strong>all</strong>) normal web developers always have references handy (HTML for Dummies, Complete All-In-One Website Reference, Garfield comic strips out of their local news paper, whatever), they don&#8217;t need to know all HTML stuff off the top of their head (and certainly don&#8217;t need to write it on paper). But this subject was nowhere near as bad as everyone&#8217;s enemy:</p>
<h3>2. Usability</h3>
<p>Falling over might be enjoyable. Jumping off a cliff could potentially be enjoyable. But this, <strong>usability</strong>, this subject is not enjoyable at all, it&#8217;s literally the worst subject. Ever. Well maybe not really, but it&#8217;s boring. Most of the class was common knowledge (you know, things like &#8220;red writing on a green background looks fugly&#8221;, &#8220;make sure users find your software interface easy to use&#8221;, and &#8220;don&#8217;t stab people&#8221;). I&#8217;m hoping we don&#8217;t get more subjects that are as annoying as this one was. If we do, I&#8217;m stabbing someone with a red and green paper. An easy-to-use one.</p>
<p>Go-karts? Did someone mention go-karts? Oh yeah, it was an idea I had when I was around 8 years old or so. I had this amazingly awesome (and by &#8220;amazingly awesome&#8221; I mean &#8220;it sounded good at the time&#8221;) idea to build a go-kart that goes along the tram lines (anyone not living in Melbourne should look up &#8220;Melbourne Trams&#8221; on Wikipedia to see these mythical &#8220;tram&#8221; thingies I&#8217;m talking about). But basically, trams are a form of public transportation here, a form of light rail. The streets have tram lines in the middle of the street, kinda like a train line except it&#8217;s on the road instead of being separate. Trams have overhead powerlines, which is what gave me this idea. Instead of the go-kart needing petrol or a battery, it could simply use the power the trams are using. Although now I realise several downfalls with this:</p>
<ul>
<li>The go-kart would only be able to go down certain paths. Not all streets have tram lines (while a lot of the streets in the CBD have tram lines, not many suburban streets do). This would restricts its mobility</li>
<li>It&#8217;d need this giant stick thing coming off of it to reach the power. Kinda like a dodgem car</li>
<li>Legalities &#8211; Would it be legal to &#8220;steal&#8221; the tram&#8217;s power? What if you hold up the trams? I can imagine my go-kart breaking down and screaming &#8220;GO AROUND!&#8221; at the trams <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />  </li>
<li>Some fourth downfall that I&#8217;ve forgotten</li>
<li>I like turtles</li>
</ul>
<p>So what else can I say? There&#8217;s not much really, my life&#8217;s pretty boring right now. Oh yes, my girlfriend and I broke up, I guess just things weren&#8217;t working out. So yeah, I&#8217;m single again. And no I&#8217;m not looking for a girlfriend at the moment, so don&#8217;t bother asking (unless you like the feeling of being rejected I guess) xD. I guess that made me realise a lot more about myself, who I am and exactly <em>what</em> I&#8217;m looking for. I made a special friend that I&#8217;m very similar and now extremely close to, it&#8217;s like we&#8217;re soulmates or something. She knows who she is, and I&#8217;m pretty certain she&#8217;d be reading this (maybe she&#8217;s the one of the 0 readers <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' />  ) <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . As a person I&#8217;ve changed pretty significantly over the past year or so (especially in the last few months), and it really seems like a pretty good thing. But I&#8217;ll save all of this for another blog post, this one&#8217;s getting rather long. I should try to blog more about life I guess, it&#8217;s something I like writing about. If you look through the firsts posts to this blog, they were pretty geeky. Then again, I&#8217;ve become way less geeky (still do programming though), haha <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />  . It all fits in to the whole &#8220;change&#8221; thing.</p>
<p>Oh, and for anyone from Australia, the NoCleanFeed protests are on the <strong>13th December</strong> (same as Swinburne results day!). For more information take a look at <a href="http://nocleanfeed.com/">NoCleanFeed.com</a>. The protests in Melbourne are going to be at the state library, and I <em>might</em> be going, I&#8217;m not sure yet. Perhaps you want some <a href="http://anotherworthlessblog.net/no-clean-feed-posters/">No Clean Feed Posters</a>, thanks to my friend Joseph. <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>Well that&#8217;s all for now&#8230;<br />
Oh and btw, the cookie&#8217;s all gone. Sorry about that.</p>
<p>Until next time <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
 &#8211; Daniel</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.cx/blog/2008/12/tram-line-go-karts-university-exams-and-more/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Using TCP sockets in Pascal, connect to remote servers</title>
		<link>http://dan.cx/blog/2008/03/using-tcp-sockets-in-pascal/</link>
		<comments>http://dan.cx/blog/2008/03/using-tcp-sockets-in-pascal/#comments</comments>
		<pubDate>Fri, 21 Mar 2008 10:59:30 +0000</pubDate>
		<dc:creator>Daniel15</dc:creator>
				<category><![CDATA[HIT1301 Portfolio]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[pascal]]></category>
		<category><![CDATA[hit1301]]></category>
		<category><![CDATA[sockets]]></category>
		<category><![CDATA[synapse]]></category>
		<category><![CDATA[tcp]]></category>
		<category><![CDATA[winsock]]></category>

		<guid isPermaLink="false">http://www.daniel15.com/blog/2008/03/21/using-tcp-sockets-in-pascal/</guid>
		<description><![CDATA[TCP sockets in Pascal are generally hard to use; Free Pascal doesn&#8217;t come with any high-level socket libraries by default, only a relatively low-level socket library. Some external libraries are available to make using sockets with Pascal easier, and one of these libraries is Synapse. Synapse is an easy-to-use socket library for Pascal, and in [...]]]></description>
			<content:encoded><![CDATA[<p>TCP sockets in Pascal are generally hard to use; Free Pascal doesn&#8217;t come with any high-level socket libraries by default, only a relatively low-level <tt>socket</tt> library. Some external libraries are available to make using sockets with Pascal easier, and one of these libraries is <strong>Synapse</strong>. Synapse is an easy-to-use socket library for Pascal, and in this blog post I&#8217;ll try to show how to use Synapse to connect to a remote server and send/receive data from it. </p>
<p><span id="more-69"></span>From <a href="http://synapse.ararat.cz/">the official Synapse site</a>:</p>
<blockquote><p>
This project deals with network communication by means of blocking (synchronous) sockets or with limited non-blocking mode. This project not using asynchronous sockets! The Project contains simple low level non-visual objects for easiest programming without problems. (no required multithread synchronisation, no need for windows message processing,…) Great for command line utilities, visual projects, NT services
</p></blockquote>
<h3>&#8220;Installing&#8221; it</h3>
<p>Firstly, you&#8217;ll want to <a href="http://synapse.ararat.cz/doku.php/download">download the stable release of Synapse</a>, and place them somewhere. At the time of writing, the latest Synapse version is release number <strong>38</strong>. Once you&#8217;ve downloaded it, extract the files somewhere (it doesn&#8217;t matter where you extract them to, as long as you remember the directory name. I&#8217;d suggest to create a directory for all your Free Pascal library code). Next, we need to edit the config file, so that Free Pascal can find these libraries. Open your Free Pascal configuration file (on Linux, this is at<tt>/etc/fpc.cfg</tt>. On Windows, this <em>should</em> be in the directory you installed Free Pascal to). Search for this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;"># searchpath for libraries</pre></div></div>

<p>Right before that, add <tt>-Fu</tt> followed by the path to the directory you made earlier. In my case, I added:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">-Fu/home/daniel/fpc</pre></div></div>

<h3>Using it in your code</h3>
<p>In most cases, you&#8217;ll be using the <tt>TTCPBlockSocket</tt> class. This is included in the <tt>blcksock</tt> unit, so add this unit to the <em>uses</em> clause of your application:</p>

<div class="wp_syntax"><div class="code"><pre class="pascal" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">uses</span> blcksock;</pre></div></div>

<h3>Example</h3>
<h4>Connecting to a server</h4>
<p>This is probably the most common way you&#8217;d use a socket &mdash; Connecting directly to another server. The functionality for this is contained in the <tt>TTCPBlockSocket</tt> class. Firstly, we need to define a variable to store the socket in:</p>

<div class="wp_syntax"><div class="code"><pre class="pascal" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span>
	sock<span style="color: #339933;">:</span> TTCPBlockSocket;</pre></div></div>

<p>And then we need to actually create the socket:</p>

<div class="wp_syntax"><div class="code"><pre class="pascal" style="font-family:monospace;">sock <span style="color: #339933;">:=</span> TTCPBlockSocket.<span style="color: #0066ee;">Create</span>;</pre></div></div>

<p>This creates a socket named <strong>sock</strong> that we&#8217;re able to use. The next step is to connect to the remote server, using the <a href="http://synapse.ararat.cz/doc/help/blcksock.TTCPBlockSocket.html#Connect">Connect</a> method:</p>

<div class="wp_syntax"><div class="code"><pre class="pascal" style="font-family:monospace;">sock.<span style="color: #0066ee;">Connect</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'66.79.183.71'</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'80'</span><span style="color: #009900;">&#41;</span>;
<span style="color: #666666; font-style: italic;">// Was there an error?</span>
<span style="color: #000000; font-weight: bold;">if</span> sock.<span style="color: #0066ee;">LastError</span> &lt;&gt; <span style="color: #cc66cc;">0</span> <span style="color: #000000; font-weight: bold;">then</span>
<span style="color: #000000; font-weight: bold;">begin</span>
	<span style="color: #000066;">writeLn</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'Could not connect to server.'</span><span style="color: #009900;">&#41;</span>;
	halt<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">end</span>;</pre></div></div>

<p>At this point, the connection to the remote server has been established, and we may send and receive data.</p>
<h4>Sending Data</h4>
<p>Sending data is done via the <a href="http://synapse.ararat.cz/doc/help/blcksock.TBlockSocket.html#SendString">SendString</a> method. Note that this is slightly different to some other languages; it <strong>does not</strong> add a carriage return and linefeed to the end of the line, you have to add this manually if required.</p>

<div class="wp_syntax"><div class="code"><pre class="pascal" style="font-family:monospace;">sock.<span style="color: #0066ee;">SendString</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'GET /blog/ HTTP/1.1'</span>#<span style="color: #cc66cc;">13</span>#<span style="color: #cc66cc;">10</span><span style="color: #ff0000;">'Host: www.daniel15.com'</span>#<span style="color: #cc66cc;">13</span>#<span style="color: #cc66cc;">10</span>#<span style="color: #cc66cc;">13</span>#<span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<h4>Receiving Data</h4>
<p>There are several methods for receiving data, but the two main ones are <tt><a href="http://synapse.ararat.cz/doc/help/blcksock.TBlockSocket.html#RecvString">RecvString</a></tt> and <tt><a href="http://synapse.ararat.cz/doc/help/blcksock.TBlockSocket.html#RecvPacket">RecvPacket</a></tt>. RecvString reads a single string (terminated by a carriage return and linefeed) from the socket, and returns this string <strong>without</strong> the carriage return. RecvPacket reads all data waiting to be read, and returns it unmodified (all carriage returns and linefeeds will still be there). Both commands take one parameter: A timeout. If the socket doesn&#8217;t contain any data within this timeout, it returns a blank string.</p>

<div class="wp_syntax"><div class="code"><pre class="pascal" style="font-family:monospace;">buffer <span style="color: #339933;">:=</span> sock.<span style="color: #0066ee;">RecvPacket</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2000</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<h4>Putting it all together</h4>
<p>Here&#8217;s an example application that connects to a web server, does a simple HTTP request, and writes the response to the console:</p>

<div class="wp_syntax"><div class="code"><pre class="pascal" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">program</span> TestApp;
&nbsp;
<span style="color: #000000; font-weight: bold;">uses</span>
	blcksock;
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span>
	sock<span style="color: #339933;">:</span> TTCPBlockSocket;
&nbsp;
<span style="color: #000000; font-weight: bold;">procedure</span> Main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span>
	buffer<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">String</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">''</span>;
<span style="color: #000000; font-weight: bold;">begin</span>
	sock <span style="color: #339933;">:=</span> TTCPBlockSocket.<span style="color: #0066ee;">Create</span>;
&nbsp;
	sock.<span style="color: #0066ee;">Connect</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'66.79.183.71'</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'80'</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #666666; font-style: italic;">// Was there an error?</span>
	<span style="color: #000000; font-weight: bold;">if</span> sock.<span style="color: #0066ee;">LastError</span> &lt;&gt; <span style="color: #cc66cc;">0</span> <span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #000000; font-weight: bold;">begin</span>
		<span style="color: #000066;">writeLn</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'Could not connect to server.'</span><span style="color: #009900;">&#41;</span>;
		halt<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">end</span>;
	<span style="color: #666666; font-style: italic;">// Send a HTTP request</span>
	sock.<span style="color: #0066ee;">SendString</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'GET /blog/ HTTP/1.1'</span>#<span style="color: #cc66cc;">13</span>#<span style="color: #cc66cc;">10</span><span style="color: #ff0000;">'Host: www.daniel15.com'</span>#<span style="color: #cc66cc;">13</span>#<span style="color: #cc66cc;">10</span>#<span style="color: #cc66cc;">13</span>#<span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span>;
&nbsp;
	<span style="color: #666666; font-style: italic;">// Keep looping...</span>
	<span style="color: #000000; font-weight: bold;">repeat</span>
		buffer <span style="color: #339933;">:=</span> sock.<span style="color: #0066ee;">RecvPacket</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2000</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #000066;">write</span><span style="color: #009900;">&#40;</span>buffer<span style="color: #009900;">&#41;</span>;
	<span style="color: #666666; font-style: italic;">// ...until there's no more data.</span>
	<span style="color: #000000; font-weight: bold;">until</span> buffer <span style="color: #339933;">=</span> <span style="color: #ff0000;">''</span>;
<span style="color: #000000; font-weight: bold;">end</span>;
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">begin</span>
	Main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">end</span>.</pre></div></div>

<p>The loop is needed because the data may come in multiple packets.</p>
<p>Not that this is <strong>not</strong> really a good example, as there&#8217;s a HTTP library built-in to Synapse. The in-built HTTP library has several advantages, including the ability to use HTTP proxies. Perhaps I&#8217;ll cover that in a future blog post <img src='http://dan.cx/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://dan.cx/blog/2008/03/using-tcp-sockets-in-pascal/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
