<?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>porges &#187; program</title>
	<atom:link href="http://porg.es/blog/tag/program/feed" rel="self" type="application/rss+xml" />
	<link>http://porg.es/blog</link>
	<description></description>
	<lastBuildDate>Thu, 12 Jan 2012 23:45:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Simple socket programming with Haskell</title>
		<link>http://porg.es/blog/simple-socket-programming-with-haskell</link>
		<comments>http://porg.es/blog/simple-socket-programming-with-haskell#comments</comments>
		<pubDate>Wed, 15 Oct 2008 01:54:35 +0000</pubDate>
		<dc:creator>Porges</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[university]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[program]]></category>
		<category><![CDATA[sockets]]></category>

		<guid isPermaLink="false">http://porg.es/blog/?p=214</guid>
		<description><![CDATA[Here is a simple program showing socket programming with Haskell, created for a University assignment. It allows half-duplex (one way at a time) communication between two (or fewer ) computers: import Network import Data.Char &#40;toLower&#41; import Text.Regex.Posix &#40;&#40;=~&#41;&#41; import System.IO &#40;hGetLine,hClose,hPutStrLn,hSetBuffering,BufferMode&#40;..&#41;,Handle,stdout&#41; &#160; port = 8001 -- a nice port number &#160; main = withSocketsDo $ [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a simple program showing socket programming with Haskell, created for a University assignment. It allows half-duplex (one way at a time) communication between two (or fewer <img src="http://porg.es/blog/wp-content/plugins/wp-smiley-switcher/noktahhitam/icon_biggrin.gif" alt="" />) computers:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">import</span> Network
<span style="color: #06c; font-weight: bold;">import</span> Data<span style="color: #339933; font-weight: bold;">.</span><span style="color: #cccc00; font-weight: bold;">Char</span> <span style="color: green;">&#40;</span>toLower<span style="color: green;">&#41;</span>
<span style="color: #06c; font-weight: bold;">import</span> Text<span style="color: #339933; font-weight: bold;">.</span>Regex<span style="color: #339933; font-weight: bold;">.</span>Posix <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">=~</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
<span style="color: #06c; font-weight: bold;">import</span> System<span style="color: #339933; font-weight: bold;">.</span><span style="color: #cccc00; font-weight: bold;">IO</span> <span style="color: green;">&#40;</span>hGetLine<span style="color: #339933; font-weight: bold;">,</span>hClose<span style="color: #339933; font-weight: bold;">,</span>hPutStrLn<span style="color: #339933; font-weight: bold;">,</span>hSetBuffering<span style="color: #339933; font-weight: bold;">,</span>BufferMode<span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">..</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span>Handle<span style="color: #339933; font-weight: bold;">,</span>stdout<span style="color: green;">&#41;</span>
&nbsp;
port <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">8001</span> <span style="color: #5d478b; font-style: italic;">-- a nice port number</span>
&nbsp;
main <span style="color: #339933; font-weight: bold;">=</span> withSocketsDo <span style="color: #339933; font-weight: bold;">$</span> <span style="color: #06c; font-weight: bold;">do</span> <span style="color: #5d478b; font-style: italic;">-- enable sockets under windows</span>
	<span style="font-weight: bold;">putStrLn</span> <span style="background-color: #3cb371;">&quot;Welcome to One-Way Chat version 1.0&quot;</span>
	hSetBuffering stdout NoBuffering <span style="color: #5d478b; font-style: italic;">-- fix buffering under windows</span>
	input <span style="color: #339933; font-weight: bold;">&lt;-</span> untilM   <span style="color: #5d478b; font-style: italic;">-- get input of 'c' or 's'</span>
		<span style="color: green;">&#40;</span>\x <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">not</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">null</span> x<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&amp;&amp;</span> toLower <span style="color: green;">&#40;</span><span style="font-weight: bold;">head</span> x<span style="color: green;">&#41;</span> `<span style="font-weight: bold;">elem</span>` <span style="background-color: #3cb371;">&quot;cs&quot;</span><span style="color: green;">&#41;</span>
		<span style="color: green;">&#40;</span><span style="font-weight: bold;">putStr</span> <span style="background-color: #3cb371;">&quot;Client or server? &quot;</span> <span style="color: #339933; font-weight: bold;">&gt;&gt;</span> <span style="font-weight: bold;">getLine</span><span style="color: green;">&#41;</span>
	<span style="color: green;">&#40;</span><span style="color: #06c; font-weight: bold;">if</span> <span style="color: green;">&#40;</span>toLower <span style="color: green;">&#40;</span><span style="font-weight: bold;">head</span> input<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">==</span> 'c'<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">then</span> client <span style="color: #06c; font-weight: bold;">else</span> server<span style="color: green;">&#41;</span> <span style="color: #5d478b; font-style: italic;">-- start the client or server</span>
		`<span style="font-weight: bold;">catch</span>` <span style="color: green;">&#40;</span><span style="font-weight: bold;">const</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">putStrLn</span> <span style="background-color: #3cb371;">&quot;Connection forced closed.&quot;</span><span style="color: green;">&#41;</span>	
	<span style="font-weight: bold;">putStrLn</span> <span style="background-color: #3cb371;">&quot;Connection closed.&quot;</span>
	<span style="font-weight: bold;">putStrLn</span> <span style="background-color: #3cb371;">&quot;Thanks for using One-Way Chat!&quot;</span> <span style="color: #5d478b; font-style: italic;">-- all done</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- reads in an ip address</span>
readIp <span style="color: #339933; font-weight: bold;">=</span> untilM	<span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">=~</span> <span style="background-color: #3cb371;">&quot;[0-9]{1,3}<span style="background-color: #3cb371; font-weight: bold;">\\</span>.[0-9]{1,3}<span style="background-color: #3cb371; font-weight: bold;">\\</span>.[0-9]{1,3}<span style="background-color: #3cb371; font-weight: bold;">\\</span>.[0-9]{1,3}&quot;</span><span style="color: green;">&#41;</span>
	<span style="color: green;">&#40;</span><span style="font-weight: bold;">putStr</span> <span style="background-color: #3cb371;">&quot;Enter IP address: &quot;</span> <span style="color: #339933; font-weight: bold;">&gt;&gt;</span> <span style="font-weight: bold;">getLine</span><span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- monadic `until`</span>
untilM p x <span style="color: #339933; font-weight: bold;">=</span> x <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> <span style="color: green;">&#40;</span>\y <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #06c; font-weight: bold;">if</span> p y <span style="color: #06c; font-weight: bold;">then</span> <span style="font-weight: bold;">return</span> y <span style="color: #06c; font-weight: bold;">else</span> untilM p x<span style="color: green;">&#41;</span> 
<span style="color: #5d478b; font-style: italic;">-- repeats two actions until either returns true</span>
while2 x y <span style="color: #339933; font-weight: bold;">=</span> ifM x <span style="color: green;">&#40;</span><span style="font-weight: bold;">return</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">$</span> ifM y <span style="color: green;">&#40;</span><span style="font-weight: bold;">return</span> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">$</span> while2 x y 
<span style="color: #5d478b; font-style: italic;">-- monadic `if`</span>
ifM p t f  <span style="color: #339933; font-weight: bold;">=</span> p <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> <span style="color: green;">&#40;</span>\p' <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #06c; font-weight: bold;">if</span> p' <span style="color: #06c; font-weight: bold;">then</span> t <span style="color: #06c; font-weight: bold;">else</span> f<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- client</span>
client <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span>
	ip <span style="color: #339933; font-weight: bold;">&lt;-</span> readIp
	<span style="font-weight: bold;">putStrLn</span> <span style="background-color: #3cb371;">&quot;Connecting...&quot;</span>
	h <span style="color: #339933; font-weight: bold;">&lt;-</span> connectTo ip <span style="color: green;">&#40;</span>PortNumber port<span style="color: green;">&#41;</span>
	<span style="font-weight: bold;">putStrLn</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="background-color: #3cb371;">&quot;Connected to &quot;</span> <span style="color: #339933; font-weight: bold;">++</span> ip <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot;:&quot;</span> <span style="color: #339933; font-weight: bold;">++</span> <span style="font-weight: bold;">show</span> port
	hSetBuffering h LineBuffering
	while2 <span style="color: green;">&#40;</span>send h<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>receive h<span style="color: green;">&#41;</span>
	hClose h
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- server</span>
server <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span>
	sock <span style="color: #339933; font-weight: bold;">&lt;-</span> listenOn <span style="color: green;">&#40;</span>PortNumber port<span style="color: green;">&#41;</span>
	<span style="font-weight: bold;">putStrLn</span> <span style="background-color: #3cb371;">&quot;Awaiting connection.&quot;</span>
	<span style="color: green;">&#40;</span>h<span style="color: #339933; font-weight: bold;">,</span>host<span style="color: #339933; font-weight: bold;">,</span>port<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&lt;-</span> accept sock
	<span style="font-weight: bold;">putStrLn</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="background-color: #3cb371;">&quot;Received connection from &quot;</span> <span style="color: #339933; font-weight: bold;">++</span> host <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot;:&quot;</span> <span style="color: #339933; font-weight: bold;">++</span> <span style="font-weight: bold;">show</span> port
	hSetBuffering h LineBuffering
	while2 <span style="color: green;">&#40;</span>receive h<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>send h<span style="color: green;">&#41;</span>
	hClose h
	sClose sock
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- sending</span>
send h <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span>
	<span style="font-weight: bold;">putStr</span> <span style="background-color: #3cb371;">&quot;Send (empty to close): &quot;</span>
	input <span style="color: #339933; font-weight: bold;">&lt;-</span> <span style="font-weight: bold;">getLine</span>
	hPutStrLn h input
	<span style="font-weight: bold;">return</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">null</span> input
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- receiving</span>
receive h <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span>
	<span style="font-weight: bold;">putStr</span> <span style="background-color: #3cb371;">&quot;Receiving: &quot;</span>
	input <span style="color: #339933; font-weight: bold;">&lt;-</span> hGetLine h
	<span style="font-weight: bold;">putStrLn</span> input
	<span style="font-weight: bold;">return</span> <span style="color: #339933; font-weight: bold;">$</span> <span style="font-weight: bold;">null</span> input</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://porg.es/blog/simple-socket-programming-with-haskell/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

