<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.0.4" -->
<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/"
	>

<channel>
	<title>Words</title>
	<link>http://slava.cyberwang.net</link>
	<description>..they most certainly are</description>
	<pubDate>Sat, 26 Apr 2008 08:13:42 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.4</generator>
	<language>en</language>
			<item>
		<title>As a system approaches absolute zero of temperature, all processes cease</title>
		<link>http://slava.cyberwang.net/2008/04/01/as-a-system-approaches-absolute-zero-of-temperature-all-processes-cease/</link>
		<comments>http://slava.cyberwang.net/2008/04/01/as-a-system-approaches-absolute-zero-of-temperature-all-processes-cease/#comments</comments>
		<pubDate>Tue, 01 Apr 2008 18:08:22 +0000</pubDate>
		<dc:creator>Slava</dc:creator>
		
	<category>Writing</category>
		<guid isPermaLink="false">http://slava.cyberwang.net/2008/04/01/as-a-system-approaches-absolute-zero-of-temperature-all-processes-cease/</guid>
		<description><![CDATA[We are, after all, just humans.
We adore and despise, we wage wars and revolutions, standing up for undefined principals on the basis of morals. Our guidance and drive derived from powers we worship, by invented beings we can never see. We build shelters and raise families to subconsciously justify our birth.
We are, after all, just [...]]]></description>
			<content:encoded><![CDATA[<p>We are, after all, just humans.</p>
<p>We adore and despise, we wage wars and revolutions, standing up for undefined principals on the basis of morals. Our guidance and drive derived from powers we worship, by invented beings we can never see. We build shelters and raise families to subconsciously justify our birth.</p>
<p>We are, after all, just animals.</p>
<p>We lust for one another, pouncing on selfish moments to indulge in primal instincts. We hunt our food through glass jungles, nearly tearing one another to shreds over concepts of property and territory. We use the world around us, as it consumes us for its sustenance.</p>
<p>We are, after all, just chemicals.</p>
<p>Our interactions filled with entropy, as we dodge the masses moving in flows through the streets. We bind together to create things unknown, in as much as we tear each other apart for undetermined causes. We attract and repel derivatives of our kind for reasons we don’t ever bother to question.</p>
<p>We are, after all, just atoms.</p>
<p>Individuals, clusters of odd and mysterious masses with composition too complex to even consider, and our being too elemental to understand ourselves. Our properties unique, yet nearly powerless without complex interactions, we drift through space seeking influence as our halos rise and fall with forces we can’t control.</p>
<p>We are, after all, just dreams.
</p>
<p><!--9261d7976c4f0ec688e2b0daf8828934-->
</p>
<p><!--bbf371b12aff75740290e60b0020013f-->
</p>
<p><!--0a25dcf202d4a3ec5b4a54221cf05363-->
</p>
<p class="tags">Tags: <a href="http://technorati.com/tag/creative" title="See the Technorati tag page for 'creative'." rel="tag">creative</a>, <a href="http://technorati.com/tag/writing" title="See the Technorati tag page for 'writing'." rel="tag">writing</a></p>]]></content:encoded>
			<wfw:commentRSS>http://slava.cyberwang.net/2008/04/01/as-a-system-approaches-absolute-zero-of-temperature-all-processes-cease/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>&#8220;There is nothing wrong with the JVM&#8221;</title>
		<link>http://slava.cyberwang.net/2008/03/24/there-is-nothing-wrong-with-the-jvm/</link>
		<comments>http://slava.cyberwang.net/2008/03/24/there-is-nothing-wrong-with-the-jvm/#comments</comments>
		<pubDate>Mon, 24 Mar 2008 13:40:09 +0000</pubDate>
		<dc:creator>Slava</dc:creator>
		
	<category>General</category>
		<guid isPermaLink="false">http://slava.cyberwang.net/2008/03/24/there-is-nothing-wrong-with-the-jvm/</guid>
		<description><![CDATA[This section will get filled in later, after work, with reasons as to why the JVM tends to be a good thing, in the post 9/11 world.







]]></description>
			<content:encoded><![CDATA[<p>This section will get filled in later, after work, with reasons as to why the JVM tends to be a good thing, in the post 9/11 world.
</p>
<p><!--f82b0f9cd7a7ee623f6b7722abf07d69-->
</p>
<p><!--bea4aed16c36a79f9099dde6955085a0-->
</p>
<p><!--a6872fee0d708c086e1ab9c30f061b2a-->
</p>
]]></content:encoded>
			<wfw:commentRSS>http://slava.cyberwang.net/2008/03/24/there-is-nothing-wrong-with-the-jvm/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>The MySpace elective withdrawal</title>
		<link>http://slava.cyberwang.net/2007/09/13/the-myspace-elective-withdrawal/</link>
		<comments>http://slava.cyberwang.net/2007/09/13/the-myspace-elective-withdrawal/#comments</comments>
		<pubDate>Thu, 13 Sep 2007 20:16:35 +0000</pubDate>
		<dc:creator>Slava</dc:creator>
		
	<category>General</category>
		<guid isPermaLink="false">http://slava.cyberwang.net/2007/09/13/the-myspace-elective-withdrawal/</guid>
		<description><![CDATA[It seems that when one cancels a MySpace account, it is expected that one fills a block with a narrative, specifying, nay, apologizing for the atrocity one is presumably committing by terminating the ability of this specific e-branch of the News Corporation from collecting exact marketing data for your demographic, and viewing what I&#8217;ve approximated [...]]]></description>
			<content:encoded><![CDATA[<p>It seems that when one cancels a MySpace account, it is expected that one fills a block with a narrative, specifying, nay, <em>apologizing</em> for the atrocity one is presumably committing by terminating the ability of this specific e-branch of the News Corporation from collecting exact marketing data for your demographic, and viewing what I&#8217;ve approximated to be a number of ads that could only be computed as a function of your screen resolution.</p>
<p>I saw this as an opportunity not to absolve myself of guilt, but to seize an opportunity to communicate with the <em>higher ups</em>, to let them know exactly why I felt my actions were justifiable, and even so, necessary:</p>
<blockquote><p>
Where to start; so many reasons, yet the textbox yields only so much space.</p>
<p>First off, the technological backing of this site seems to be comprised entirely out of dot matrix printers handled by minimum wage, half-blind alcoholics who will, on occasion, deliver a request to someone who can begin to service a reply.</p>
<p>Honestly, for what the competing social network sites have managed, even with asynchronous calls, you can&#8217;t seem to handle on a daily basis with the most straight forward GET-driven request.  Your code base has sucked too many times to remain enjoyable, purely on the concept of being unresponsive and apologetic for its incompetence, but never repaired.</p>
<p>Secondly, the implemented spam filters, if any, are a joke, and I would consider further an abortion, when compared to any other such system. In the recent past my usage experience consisted solely of clearing my inbox of automated requests to check out her “hot new super profile,” then promptly logging out, because accomplishing much more involves dealing with your half-assed barely operational back-end architecture.</p>
<p>Thirdly, your profile editors are an invitation for unwarranted remote code execution on the negative side of the spectrum, and an enormous resource drain on the positive side, as the only people to ever use the feature are fourteen year olds bent on either world conquest via worm, or over-stylized hyper shiny multi-layer transparency.</p>
<p>In conclusion, your operation has gone from one of the leading social networks, to a virtual cesspool of garbage, and I see no turning back. If anything, I wish that you not cease, nor improve what you do.  But keep on keeping on, hoping that you will continue to attract generations of half-witted teenagers who will at the very least, stay off the rest of the internet.
</p>
</blockquote>
<p>Posted here because, I&#8217;m told, it&#8217;s relatively interesting or entertaining to read.
</p>
<p><!--4827c6fd3399f53fe6b7e08399d9b118-->
</p>
<p><!--4cd125b5c494b98aa66444528ae21a85-->
</p>
<p class="tags">Tags: <a href="http://technorati.com/tag/myspace" title="See the Technorati tag page for 'myspace'." rel="tag">myspace</a></p>]]></content:encoded>
			<wfw:commentRSS>http://slava.cyberwang.net/2007/09/13/the-myspace-elective-withdrawal/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>A Recent Picture</title>
		<link>http://slava.cyberwang.net/2007/01/09/a-recent-picture/</link>
		<comments>http://slava.cyberwang.net/2007/01/09/a-recent-picture/#comments</comments>
		<pubDate>Tue, 09 Jan 2007 04:13:40 +0000</pubDate>
		<dc:creator>Slava</dc:creator>
		
	<category>General</category>
		<guid isPermaLink="false">http://slava.cyberwang.net/2007/01/09/a-recent-picture/</guid>
		<description><![CDATA[
.flickr-photo { border: solid 2px #000000; }
.flickr-yourcomment { }
.flickr-frame { text-align: left; padding: 3px; }
.flickr-caption { font-size: 0.8em; margin-top: 0px; }


	
A Recent Picture, originally uploaded by Muta.


	I haven&#8217;t had a good recent picture, so I&#8217;m going to post this one as I figure it looks nice.





]]></description>
			<content:encoded><![CDATA[<p><style type="text/css"><br />
.flickr-photo { border: solid 2px #000000; }<br />
.flickr-yourcomment { }<br />
.flickr-frame { text-align: left; padding: 3px; }<br />
.flickr-caption { font-size: 0.8em; margin-top: 0px; }<br />
</style></p>
<div class="flickr-frame">
	<a href="http://www.flickr.com/photos/tehaids/351146713/" title="photo sharing"><img src="http://farm1.static.flickr.com/126/351146713_ef315056da.jpg" class="flickr-photo" alt="" /></a></p>
<p><span class="flickr-caption"><a href="http://www.flickr.com/photos/tehaids/351146713/">A Recent Picture</a>, originally uploaded by <a href="http://www.flickr.com/people/tehaids/">Muta</a>.</span>
</div>
<p class="flickr-yourcomment">
	I haven&#8217;t had a good recent picture, so I&#8217;m going to post this one as I figure it looks nice.
</p>
<p><!--51a61ae84d96ab5e26719c51ce4cd857-->
</p>
<p><!--b215622b4671c2c3bdf6408877142c6d-->
</p>
]]></content:encoded>
			<wfw:commentRSS>http://slava.cyberwang.net/2007/01/09/a-recent-picture/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>On Clarity</title>
		<link>http://slava.cyberwang.net/2007/01/04/on-clarity/</link>
		<comments>http://slava.cyberwang.net/2007/01/04/on-clarity/#comments</comments>
		<pubDate>Thu, 04 Jan 2007 07:36:18 +0000</pubDate>
		<dc:creator>Slava</dc:creator>
		
	<category>Programming</category>
		<guid isPermaLink="false">http://slava.cyberwang.net/2007/01/04/on-clarity/</guid>
		<description><![CDATA[As a software developer, I&#8217;ve gained the privilege of looking at countless lines of code, some my own and some inherited from others.  Many times, I&#8217;m not just browsing for pleasure, but either debugging or doing further development on these fragments of logic, and sometimes the message being conveyed to the compiler is not [...]]]></description>
			<content:encoded><![CDATA[<p>As a software developer, I&#8217;ve gained the <em>privilege</em> of looking at countless lines of code, some my own and some inherited from others.  Many times, I&#8217;m not just browsing for pleasure, but either debugging or doing further development on these fragments of logic, and sometimes the message being conveyed to the compiler is not as clear to a mere human reader.</p>
<p>I&#8217;m in no way implying that I&#8217;m perfect, and that whatever this article shapes up to be should be the definition, or law of clear code, but the guidelines here are ones that I use, and so far have proven useful to me and those who have chosen to adopt them.</p>
<p>I will subdivide this article into headings describing issues I see as being problematic areas and either proposing a solution, or commenting on a situation where a solution may not be so clear cut.</p>
<p><strong>Paragraphing</strong></p>
<p>Much like in English, blocks of code are not monolithic entities, in several cases a single function or method does several <em>things</em> to accomplish it&#8217;s goal, and many times readability increases dramatically if these <em>things</em> are separated into logical sections by whitespace.  For a totally out of context example consider the following block:</p>
<blockquote>
<pre>
SomeClass scObject = new SomeClass();
scObject->gatherInformation();
if(!scObject->infoValid()) {
    delete csObject;
    throw new someException();
}
RandomStaticClass::transformObject(scObject);
for(scObject::someIterator scIter = scObject->begin(); scIter != scObject->end(); scIter++) {
    arbitraryFunction(*scIter);
}
return scObject;
</pre>
</blockquote>
<p>Despite the hilarious naming, this simple block of code is pretty simple to understand.  It creates an object, calls a method, verifies the information, performs some transformation then iterates <em>something</em> before returning the object to the caller.  But in the reading of the code, there needs to be some concentration on behalf of the reader to separate these individual sections mentally before fully understanding their functions within the given context.  In my opinion, and further in practice I find code in such close concentration more often than I care to count, and due to my already present mental breaking down of the sections I&#8217;ve chosen to adapt a practice of physically separating these sections into paragraph-like blocks based on function, transforming the above code, into the below code:</p>
<blockquote>
<pre>
SomeClass scObject = new SomeClass();

scObject->gatherInformation();

if(!scObject->infoValid()) {
    delete csObject;
    throw new someException();
}

RandomStaticClass::transformObject(scObject);

for(scObject::someIterator scIter = scObject->begin(); scIter != scObject->end(); scIter++) {
    arbitraryFunction(*scIter);
}

return scObject;
</pre>
</blockquote>
<p>The extra whitespace does nothing functionally, but it forces the separation of the logical sections for, in my opinion, easier reading.</p>
<p><strong>Clever Only Commenting</strong></p>
<p>The drive in programming has been for verbose commenting, and although I support this in most cases, such as documentation of methods and very complex sections, I don&#8217;t agree with the blanket statement, as the resulting code is far less readable than it could have been if comments were removed all together.   I base my theory on the idea that people who write code are familiar with the language in which it was written, as well as with the science of software development in general.  Like many people familiar with their field for developers many patterns are recalled instantly, and many algorithms are easily identified by their signature within a construct and any additional verbosity is simply noise surrounding signal.</p>
<p>To illustrate my point in psuedocode once more:</p>
<blockquote>
<pre>
//Declare an integer
int i = 0; //It will be initialized to zero

//Loop 10 times
//Re-init i for style
for(i = 0; i < 10; i++) {
    doSomething(i); //Call doSomething() function with the current value of i
} //End of for(0..10) loop on i

//Create a new char pointer of size 10
char *cpBuffer = new char[10];
//Clear the section we just allocated
bzero(cpBuffer, 10);
//Pass the buffer to another method of a static class
//SomeStaticClass will use this buffer for subsequent static calls.
SomeStaticClass::setBuffer(cpBuffer);
//Return the buffer to the caller
return cpBuffer;
</pre>
</blockquote>
<p>Although the volume and content of the comments seems almost comical, this example is not far removed from reality, mostly appearing in examples, but rarely making it to production code as well.  My concern though, is not trivial even given these limitations, for if people are learning through such verbose examples there is a chance that it will considered kosher and used in projects outside the realm of hobby.</p>
<p>What I ask, is that the above code be compared to the code below by someone familiar with even the basics of the language:</p>
<blockquote>
<pre>
int i = 0;

for(i = 0; i < 10; i++) {
    doSomething(i);
}

char *cpBuffer = new char[10];

bzero(cpBuffer, 10);

//SomeStaticClass will use this buffer for subsequent static calls.
SomeStaticClass::setBuffer(cpBuffer);

return cpBuffer;
</pre>
</blockquote>
<p>Although the code is of trivial complexity, the verbose comments made very little impact on readability.  In fact, I&#8217;m of the opinion that the latter example is much simpler to understand, and reads much faster than its comment-heavy brother.</p>
<p>The real point is that the second block is not stripped of all comments.  The section passing the buffer to a static class is still prefaced with a comment explaining why the action is being taken.  In this fictional example, that section of code stands out because its function is not as apparent as its surroundings, and as such it is explained with a simple statement.</p>
<p>This rule is harder to apply on a broader scale for several reasons.  The use of &#8220;clever&#8221; blocks is relative to the skill and project familiarity of the team.  Where seasoned developers can easily spot and track complex recursion, it may be considered clever by those who have not dealt with it constantly in a previous project, so the application of this rule may need to based on arbitrary set of standards which set the bar for what behavior is considered &#8220;clever&#8221; and deserving explanation, and by no means do I advocate the absence of full documentation on the project, including documentation of method actions, returns, and conditions.  The statements I make here are touching only on comments made inline to explain small sections of code.</p>
<p><strong>Variable Naming</strong></p>
<p>A classic point, made in every single style guide I&#8217;ve ever seen, and yet still seems to be an elusive dream for the majority of functioning code on this planet.  </p>
<p>As developers, we are not bound to the rules of algebra and as such are free to use variable names that exceed the scope of single letters.  But simply giving a descriptive name does not solve the problem.  As seen in earlier examples here, I&#8217;m a fan of <a href="http://en.wikipedia.org/wiki/Hungarian_notation">hungarian notation</a>, but even that can get far out of hand.  Although a great idea, the concept of 10-odd letter prefixes are just as frightening as none at all, and as all good things should be used in moderation:</p>
<blockquote>
<pre>
for(unsigned int uiTempIndexVariable = 0; uiTempIndexVariable < 10; uiTempIndexVariable++) {
    SomeStaticClass::updateDataIndex(uiTempIndexVariable);
}
</pre>
</blockquote>
<p>As demonstrated, the abuse of this suggestion is more harmful than simply its omission, further the use of Hungarian notation is subject to taste, but my personal reasons revolve mostly around seldom visited regions of code.  Given a block of code in which variable declarations are either at class scope, or are off-screen during an edit, type prefixes as well as descriptive names are a very useful reminder as to what can, and should be done with data, and despite the fact that you may be the original author, several months from creation the specifics of code drift from memory.</p>
<p>Yet as to most, there is an exception which I&#8217;ve demonstrated with the loop above.  I see no point for short-scoped loop indexes, or return stores to have names more descriptive than &#8216;ret&#8217; or the familiar set of &#8216;i, j, k, l&#8217; and so forth.  If a variable lives no longer than it&#8217;s declaration, or simply used in a tight scope the name is often times glossed over in reading, and this process should not be disturbed as it would aide in nothing but the degradation of reading speed with no benefit to clarity.</p>
<p><strong>Useful Commit Messages</strong></p>
<p>As a developer, one easily falls into a deep respect for source control.  It will allow you to fix your mistakes, and to work on a team without the utter destruction of sanity, but even that wonderful system can be reduced to a useless heap of data with careless human intervention.   As you should be familiar with, every commit to any source control system requires a “Commit Message” or a short blurb describing what changes have been made that made the commit worth happening.  And as often the case, shortcuts are taken.  Yet unlike all other areas of laziness, commit message shortcuts aid in no way to the reduction of work.  Quite conversely, they add work to later cases.</p>
<p>Most reduced commit messages read with the finesse of statements such as “stuff” or “some changes” or even more often, yet equally useless “”, the blank string.  But by using these non-informational descriptions work is created further down the line, when it comes time to decipher what “stuff” was changed, or what one was thinking when one changed the code to the tune of “”.  And as most humans would agree, a one line message such as “Fixed typo in variable names” is much easier, and not to mention faster to read than a diff.</p>
<p><!--1671c08b544522f32ee6427c5c0979f1-->
</p>
<p><!--89d5a9b37fcb5f9797265f3c47c92a35-->
</p>
<p class="tags">Tags: <a href="http://technorati.com/tag/programming" title="See the Technorati tag page for 'programming'." rel="tag">programming</a>, <a href="http://technorati.com/tag/c++" title="See the Technorati tag page for 'c  '." rel="tag">c  </a>, <a href="http://technorati.com/tag/code" title="See the Technorati tag page for 'code'." rel="tag">code</a>, <a href="http://technorati.com/tag/style" title="See the Technorati tag page for 'style'." rel="tag">style</a></p>]]></content:encoded>
			<wfw:commentRSS>http://slava.cyberwang.net/2007/01/04/on-clarity/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Trackbacks, Bastards and Blocklists</title>
		<link>http://slava.cyberwang.net/2006/11/19/trackbacks-bastards-and-blocklists/</link>
		<comments>http://slava.cyberwang.net/2006/11/19/trackbacks-bastards-and-blocklists/#comments</comments>
		<pubDate>Sun, 19 Nov 2006 10:49:34 +0000</pubDate>
		<dc:creator>Slava</dc:creator>
		
	<category>Meta</category>
		<guid isPermaLink="false">http://slava.cyberwang.net/2006/11/19/trackbacks-bastards-and-blocklists/</guid>
		<description><![CDATA[As of recent, most particularly, last week I&#8217;ve recieved several hundred trackbacks to a collection of articles on this page.  Had the situation that I&#8217;m about to describe been different such circumstances would be celebrated as oposed to being condemned and placed under a heading containing the word &#8220;Bastards,&#8221; but this situation fits the [...]]]></description>
			<content:encoded><![CDATA[<p>As of recent, most particularly, last week I&#8217;ve recieved several hundred trackbacks to a collection of articles on this page.  Had the situation that I&#8217;m about to describe been different such circumstances would be celebrated as oposed to being condemned and placed under a heading containing the word &#8220;Bastards,&#8221; but this situation fits the title wonderfully.</p>
<p>Essentially, all the trackbacks orginated from a single provider: inhoster.com</p>
<p>This host, from the best of my understanding contains no legitimate customers, and serves only as a cover for countless spammers running an innumerable number of spam bots.  Lucky for me, and potentially the metaphorical <i>us</i> the trackbacks originated from a very narrow host of addresses.  After several attempts, and filtering with a moderation queue my current blocklist and moderation filter contain just three entries, which for your convinience I will post here:</p>
<blockquote><p>
85.255.114<br />
85.255.119<br />
85.255.113
</p>
</blockquote>
<p>Feel free to add these to your block lists, or at the very least your moderation filter to stop these potential bastards from wasting your juicy bandwidth.</p>
<p><!--9d33cdf4ba2144abe059362f41538e1d-->
</p>
<p><!--f69d5d41bd2e94f3d6118842e5da78a3-->
</p>
<p class="tags">Tags: <a href="http://technorati.com/tag/spam" title="See the Technorati tag page for 'spam'." rel="tag">spam</a>, <a href="http://technorati.com/tag/trackback" title="See the Technorati tag page for 'trackback'." rel="tag">trackback</a>, <a href="http://technorati.com/tag/inhoster" title="See the Technorati tag page for 'inhoster'." rel="tag">inhoster</a></p>]]></content:encoded>
			<wfw:commentRSS>http://slava.cyberwang.net/2006/11/19/trackbacks-bastards-and-blocklists/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Unix Network Programming &#8212; Why it sucks: Part 1</title>
		<link>http://slava.cyberwang.net/2006/10/03/unix-network-programming-why-it-sucks-part-1/</link>
		<comments>http://slava.cyberwang.net/2006/10/03/unix-network-programming-why-it-sucks-part-1/#comments</comments>
		<pubDate>Wed, 04 Oct 2006 00:00:58 +0000</pubDate>
		<dc:creator>Slava</dc:creator>
		
	<category>Programming</category>
	<category>Articles</category>
		<guid isPermaLink="false">http://slava.cyberwang.net/2006/10/03/unix-network-programming-why-it-sucks-part-1/</guid>
		<description><![CDATA[Network programming, in specific, network programming with BSD sockets is somewhat a strange science.  Not many topics are covered in as many old libraries retained for &#8220;historical reasons&#8221; and oddities of dealing with so many strange conditions that I&#8217;ve ever had the pleasure of dealing with.  In this N-part series I will attempt to explain [...]]]></description>
			<content:encoded><![CDATA[<p>Network programming, in specific, network programming with BSD sockets is somewhat a strange science.  Not many topics are covered in as many old libraries retained for &#8220;historical reasons&#8221; and oddities of dealing with so many strange conditions that I&#8217;ve ever had the pleasure of dealing with.  In this N-part series I will attempt to explain some of the strange requirements as well as the common mistakes while giving an introduction to this fun and exciting topic.</p>
<p>Now, many books, articles and various other sources of information on the topic exist, so why should I create another one?  Mostly to blend the line beteen refference and tutorial, as well as intermix the topics of TCP/IP, which are essentially required knowledge for functioning applications.  But in reality, the only reason I&#8217;m writing this is for my own amusement, so if something comes from it, good for all, if not, then still good for me.  If during the course of this article, you notice a mistake, either technical, gramatical or pretty much any other case, please leave me a comment, I will review it, and while weeping silently for hours, correct it and attempt to bring honor back into my life.</p>
<p>So without much more rambling, we&#8217;ll move ahead to a definition of the problem at hand.<br />
<a id="more-4"></a><br />
Sockets at its core, is an API that allows easy access to the networking components of your enviornment, in its distilled form a socket is just a file handle, but instead of being bound to disk I/O operations, it is directed to a different process, which may or may not reside on the same system.  To understand at least the fundamentals of this concept without getting into the details of low level IP frames as most books would, I will simply explain the relevant parts of this exchange.</p>
<p>As most are aware, most communication on modern networks takes place over IP, the protocol on which TCP, UDP and a short bus full of other acronyms ride.  This protocol defines what is called a &#8220;Frame&#8221; or a &#8220;Packet&#8221; in the header of this bit of data is  relevant information about the source, destination, and various flags that may be set for a specific chunk of data.  I will cover this in detail in a later article, complete with a diagram, but for now we will only concern ourselves with the source, and destination addresses.</p>
<p>Those more in the know, without specific knowledge will begin screaming about &#8220;ports&#8221; which, at this layer do not exist, yet.</p>
<p>The source and destination addresses are 32-bit fields each (in IPv4, 128-bit in IPv6) which determine where this IP frame is going, and to whom the receiving host should reply.  The actual path the packet takes would be better described in a different article, as volumes have been written and millions have been made determining the answer to that question placing it far outside the scope of this introction.</p>
<p>On top of the IP frame, sit the aforementioned acronyms of UDP, TCP and several others.  It will be at this point, that I will break convention, and convinience and discuss the lesser known UDP.</p>
<p>UDP, or &#8216;User Datagram Protocol&#8217; is a stateless and simple protocol with very light overhead.  It is with this that I will describe the importance of ports, before suddenly removing myself from the subject to lunge at the actual libraries and ritual incantations involved in getting a networked application off the ground.</p>
<p>The TCP and UDP protocols define what is known as a &#8220;port&#8221; on top (or more correctly, below) of the IP defined &#8220;address&#8221; to determine the final resting place of a frame of data.  Both protocals have a combination of a &#8220;source port&#8221; and a &#8220;destination port&#8221; fields, both 16-bits in length.</p>
<p>The reason for &#8220;ports&#8221; to reside on top of an existing address structure is to make multiple streams of communication possible.  While I find this often described with metaphors, I will leave the creative thinking to the reader, while I fall back on a plain and simple explanation.  On a host, there are potentialy very many streams of communication with one or many hosts.  While it is obvious, with the prior mention of a source and destination address, how a host may know from which remote host a packet arrived, it is a far more challenging problem to simply &#8220;guess&#8221; what the hell the remote host is on about in that specific packet.  The system of ports allows the trafic to travel to the host marked with a numeric representation of where it must go.  The received packet is filed into a specific port, which a program has opened for that specific application to process.</p>
<p>Now knowing the basics, I can introduce the first, and arguably most important data structure in use in socket programming. sockaddr_in.  It is defined in the &lt;netinet/in.h&gt; header and looks like this:</p>
<blockquote>
<pre>
struct sockaddr_in {
    uint8_t sin_len;
    sa_family_t sin_family;
    in_port_t sin_port;
    struct  in_addr sin_addr;
    char sin_zero[8];
};
</pre>
</blockquote>
<p>Knowing what we already know, some of those fields become obvious, but even in those lie quirks, and &#8220;historical reasons&#8221; so let&#8217;s go through them one by one:</p>
<p><b>uint8_t sin_len:</b>  This is the least annoying, and most straight forward of the fields, containing simply the length of the struct, this is usually set with a sizeof(struct sockaddr_in)</p>
<p><b>sa_family_t sin_family:</b>  This specifies the family of the socket, for almost all applications, this value will be set to <i>AF_INET</i> which covers TCP and UDP protocols, but in rare cases, which I wont cover unless I get to them, this value may be different. (IPv6 requires AF_INET6, but also requires a different structure because of the address size and other members, this may be covered in detail at a later article)</p>
<p><b>in_port_t sin_port:</b> This is a 16 bit value in network byte order containing the port of this socket.</p>
<p><b>struct  in_addr sin_addr</b> This is the binary address of the remote host, a 32 bit value.  This is also the first case of &#8220;historical reason.&#8221;  If you note, this is not a simple type, but a struct. On the other side of the struct, is this gem:</p>
<blockquote>
<pre>
struct in_addr {
    in_addr_t s_addr;
};
</pre>
</blockquote>
<p>It is within this struct, that the value for the address is actually set in network byte order.</p>
<p><b>char sin_zero[8]:</b> As the name implies, this field is filled with zeros.</p>
<p>Now if you&#8217;ve paid attention, you have noticed a mention of something called &#8220;network byte order.&#8221;  This concept becomes very significant in this field as all computers communicating across a network expect it.</p>
<p>Network byte order may be better recognized as &#8220;big endian&#8221; byte order.  This would not be such a problem, had byte orders been a standard among architectures, which is not the case.  Intel machines use the oposite &#8220;little endian&#8221; order.  The distinction is that numbers in excess of 1 byte in length are stored with either the highest order byte first (in the lowest memory location) in big endian, or the opposite in little endian.</p>
<p>While binary information <i>can</i> be sent across a network in the native byte order, you will run into problems communicating across different architectures that expect the byte order to be different, and will interpret the numerical data in a very different way.</p>
<p>The functions that exist to convert from native to network byte orders and back are defined along side with the rest of the sockets library in the header &lt;arpa/inet.h&gt;.  These functions are as follows:</p>
<blockquote>
<pre>
unsigned long htonl(unsigned long)
unsigned short htons(unsigned short)
unsigned long ntohl(unsigned long)
unsigned short ntohs(short)
</pre>
</blockquote>
<p>They all follow the same naming convention, and follow the same rules of use.  They accept an integer, either a long, or a short, and covert it into the same type, but in network byte order.  These functions still exist on big endian architectures, but they do nothing.  Even if you are writing your code on one of the big endian architectures and don&#8217;t forsee deployment on other architectures it is still good habit to use these calls.</p>
<p>The naming convention for the conversion function is as follows:  <i>h</i> or <i>n</i>, stands for &#8220;host&#8221; or &#8220;network&#8221; byte order, where host is the native computer&#8217;s byte order, then the word &#8220;to&#8221; implying a conversion, followed by another, and opposite letter stating the order to convert to, followed by a letter specifiying wether the call will convert a short, or a long integer.</p>
<p>For example, let&#8217;s take htons():<br />
It begins with an <i>h</i>, so it accepts the native or &#8220;host&#8221; byte order, and the final two letters are: <i>ns</i>.  The fact that it would convert to network byte order is obvious, because it accepts the opposite, and because it notes so in the final two letters with the <i>n</i> and the type is specified with the final letter <i>s</i> which dictates that it accepts, and returns a short (16-bit) value.</p>
<p>Armed with all of this knowledge we are finaly able to fill out a full <i>sockaddr_in</i> structure.  This is useful in several cases such as the <i>bind()</i>, <i>connect()</i> calls.</p>
<p>First, we have to define it.  This is simple enough, we just declare it and give it a name:</p>
<blockquote>
<pre>
struct sockaddr_in saPeer;
</pre>
</blockquote>
<p>The next step, which is usually the easiest, and least painfull way to make sure sin_zero is filled is to zero out the entire structure.  Adding a call to bzero()  from &lt;strings.h&gt; will take care of this:</p>
<blockquote>
<pre>
struct sockaddr_in saPeer;
bzero(saPeer, sizeof(struct sockaddr_in));
</pre>
</blockquote>
<p>We also need to set the <i>sin_len</i> field to the size of the sockaddr_in structure.  This accomplished simply by calling sizeof() with the type of the data:</p>
<blockquote>
<pre>
struct sockaddr_in saPeer;
bzero(saPeer, sizeof(struct sockaddr_in));

saPeer.sin_len = sizeof(struct sockaddr_in);
</pre>
</blockquote>
<p>And now we have to fill in the fields, the structure we&#8217;re filling now will be used to recieve a connection on port 9000 from any interface the host has available, so we will use a special constant: <i>INADDR_ANY</i> in the <i>sin_addr</i> field.  When we cover writing a client, we will revisit this section to show how this structure is used in outgoing connections.</p>
<p>The first thing that we will set is the procol family in the <i>sin_family</i> field.  The family you will be using most of the time for standard network applications is <i>AF_INET</i>.  This family includes TCP, UDP and RAW sockets. </p>
<blockquote>
<pre>
struct sockaddr_in saPeer;
bzero(saPeer, sizeof(struct sockaddr_in));

saPeer.sin_len = sizeof(struct sockaddr_in);
saPeer.sin_family = AF_INET;
</pre>
</blockquote>
<p>Progressing down the datastructure, we can now set the address, and the port.  Remember to use network byte order for values originating from the host&#8217;s architecture:</p>
<blockquote>
<pre>
struct sockaddr_in saPeer;
bzero(saPeer, sizeof(struct sockaddr_in));

saPeer.sin_len = sizeof(struct sockaddr_in);
saPeer.sin_family = AF_INET;
saPeer.sin_addr.s_addr = INADDR_ANY;
saPeer.sin_port = htons(9000);
</pre>
</blockquote>
<p>If you notice, I warned against using the host&#8217;s byte order while at the same time using a constant without passing it through an htonx() funxtion.  This is because constants defined by the library are formatted to already be in the required byte order, and most of the time the contents of those constants are of no importance to people not messing with the actual TCP/IP stack of the operating system.</p>
<p>To review: In this section we see an explanation of the basic concepts required to grasp the construction of the base datatype in use, the <i>sockaddr_in</i> structure.  We saw an overview of byte orders, and their role in network programming, and then using all of the collected knowledge built a <i>sockaddr_in</i> structure that could be used to write a network server.</p>
<p>In the next part of this series we will be picking up at this very point, and use the structure we constructed to write a simple echo server capable of accepting one client at a time.
</p>
<p><!--9ed66bb3b12920f86c8a1ba3e57f1a34-->
</p>
<p><!--8b96e3b0cf8de496a7f5abb3ff02ed91-->
</p>
<p><!--8f44322bbb1244b2d131cd400e00352f-->
</p>
<p><!--9ed66bb3b12920f86c8a1ba3e57f1a34-->
</p>
<p><!--8f44322bbb1244b2d131cd400e00352f-->
</p>
<p class="tags">Tags: <a href="http://technorati.com/tag/network" title="See the Technorati tag page for 'network'." rel="tag">network</a>, <a href="http://technorati.com/tag/bsd" title="See the Technorati tag page for 'bsd'." rel="tag">bsd</a>, <a href="http://technorati.com/tag/sockets" title="See the Technorati tag page for 'sockets'." rel="tag">sockets</a>, <a href="http://technorati.com/tag/programming" title="See the Technorati tag page for 'programming'." rel="tag">programming</a></p>]]></content:encoded>
			<wfw:commentRSS>http://slava.cyberwang.net/2006/10/03/unix-network-programming-why-it-sucks-part-1/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>I&#8217;m back.</title>
		<link>http://slava.cyberwang.net/2006/09/25/im-back/</link>
		<comments>http://slava.cyberwang.net/2006/09/25/im-back/#comments</comments>
		<pubDate>Mon, 25 Sep 2006 18:04:02 +0000</pubDate>
		<dc:creator>Slava</dc:creator>
		
	<category>General</category>
		<guid isPermaLink="false">http://slava.cyberwang.net/2006/09/25/im-back/</guid>
		<description><![CDATA[This blog&#8217;s been missing for a while now.
And now it&#8217;s back up on a new host, with everything that was ever here deleted.
Isn&#8217;t that FUN?
Update: Added tags, and LJ crosspost (HI!)





Tags: first, back, cyberwang]]></description>
			<content:encoded><![CDATA[<p>This blog&#8217;s been missing for a while now.</p>
<p>And now it&#8217;s back up on a new host, with everything that was ever here deleted.</p>
<p>Isn&#8217;t that FUN?</p>
<p><strong>Update: </strong>Added tags, and LJ crosspost (HI!)
</p>
<p><!--3f49bdde89ecae7edc22f9cdb19a961c-->
</p>
<p><!--6c047eaea17ee68f08c7d444107fc765-->
</p>
<p class="tags">Tags: <a href="http://technorati.com/tag/first" title="See the Technorati tag page for 'first'." rel="tag">first</a>, <a href="http://technorati.com/tag/back" title="See the Technorati tag page for 'back'." rel="tag">back</a>, <a href="http://technorati.com/tag/cyberwang" title="See the Technorati tag page for 'cyberwang'." rel="tag">cyberwang</a></p>]]></content:encoded>
			<wfw:commentRSS>http://slava.cyberwang.net/2006/09/25/im-back/feed/</wfw:commentRSS>
		</item>
	</channel>
</rss>
