<?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; pascal</title>
	<atom:link href="http://dan.cx/blog/category/pascal/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>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>
