<?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>Coder-Studio &#187; telnet</title>
	<atom:link href="http://www.coder-studio.com/blog/tag/telnet/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.coder-studio.com/blog</link>
	<description></description>
	<lastBuildDate>Wed, 31 Mar 2010 00:00:53 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>(Petit) Clône de client telnet en Haskell</title>
		<link>http://www.coder-studio.com/blog/petit-clone-de-client-telnet-en-haskell/</link>
		<comments>http://www.coder-studio.com/blog/petit-clone-de-client-telnet-en-haskell/#comments</comments>
		<pubDate>Fri, 28 Aug 2009 11:19:08 +0000</pubDate>
		<dc:creator>Alp Mestan</dc:creator>
				<category><![CDATA[Langages fonctionnels]]></category>
		<category><![CDATA[fonctionnel]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[telnet]]></category>

		<guid isPermaLink="false">http://www.coder-studio.com/blog/?p=421</guid>
		<description><![CDATA[Bonjour,
L&#8217;autre nuit, muni de café, j&#8217;ai souhaité m&#8217;amuser avec Haskell. J&#8217;ai alors consulté le chapitre sur le réseau en Haskell de Real World Haskell et&#8230; j&#8217;ai écrit un (très petit) clône de client telnet&#8230; qui fait 41 lignes. Le seul soucis étant que dans toute application de ce genre, on doit partager habilement la lecture [...]]]></description>
			<content:encoded><![CDATA[<p>Bonjour,</p>
<p>L&#8217;autre nuit, muni de café, j&#8217;ai souhaité m&#8217;amuser avec Haskell. J&#8217;ai alors consulté le chapitre sur le réseau en Haskell de <a href="http://book.realworldhaskell.org/read/">Real World Haskell</a> et&#8230; j&#8217;ai écrit un (très petit) clône de client telnet&#8230; qui fait 41 lignes. Le seul soucis étant que dans toute application de ce genre, on doit partager habilement la lecture des entrées de l&#8217;utilisateur, et l&#8217;affichage de ce que l&#8217;on nous envoie. Ceci mis à part, tout cela fonctionne très bien !</p>
<p><center><img src="http://haskell.org/logos/logos/logo7000.png" alt="Haskell" title="Haskell" /></center></p>
<p><span id="more-421"></span></p>
<p>Regardons à quoi cela ressemble. Petit détail : à chaque tour, je lance la réception de données réseau dans un autre thread, pour ne pas bloquer l&#8217;écriture de l&#8217;utilisateur.</p>
<p>On importe d&#8217;abord les modules nécessaires.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p421code8'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4218"><td class="code" id="p421code8"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">import</span> Control<span style="color: #339933; font-weight: bold;">.</span>Concurrent 
<span style="color: #06c; font-weight: bold;">import</span> Control<span style="color: #339933; font-weight: bold;">.</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Monad"><span style="color: #cccc00; font-weight: bold;">Monad</span></a> 
<span style="color: #06c; font-weight: bold;">import</span> Network<span style="color: #339933; font-weight: bold;">.</span>Socket 
<span style="color: #06c; font-weight: bold;">import</span> Network<span style="color: #339933; font-weight: bold;">.</span>BSD 
<span style="color: #06c; font-weight: bold;">import</span> System<span style="color: #339933; font-weight: bold;">.</span>Environment 
<span style="color: #06c; font-weight: bold;">import</span> System<span style="color: #339933; font-weight: bold;">.</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #cccc00; font-weight: bold;">IO</span></a></pre></td></tr></table></div>

<p>Ensuite, j&#8217;ai écrit quelques fonctions pour rendre la connection, l&#8217;envoi etc plus faciles.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p421code9'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4219"><td class="code" id="p421code9"><pre class="haskell" style="font-family:monospace;"><span style="color: #5d478b; font-style: italic;">-- Ouverture d'une connexion TCP cliente </span>
<span style="color: #5d478b; font-style: italic;">-- Prend en argument l'host et le port/service (qui est une String également) </span>
<span style="color: #5d478b; font-style: italic;">-- Retourne le &quot;Handle&quot; vers le socket en question. </span>
openConnection <span style="color: #339933; font-weight: bold;">::</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> ServiceName <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #cccc00; font-weight: bold;">IO</span></a> Handle 
openConnection hostname port <span style="color: #339933; font-weight: bold;">=</span> 
  <span style="color: #06c; font-weight: bold;">do</span> 
  addrInfos <span style="color: #339933; font-weight: bold;">&lt;-</span> getAddrInfo Nothing <span style="color: green;">&#40;</span>Just hostname<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>Just port<span style="color: green;">&#41;</span> 
  <span style="color: #06c; font-weight: bold;">let</span> serveraddr <span style="color: #339933; font-weight: bold;">=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:head"><span style="font-weight: bold;">head</span></a> addrInfos 
  sock <span style="color: #339933; font-weight: bold;">&lt;-</span> socket <span style="color: green;">&#40;</span>addrFamily serveraddr<span style="color: green;">&#41;</span> Stream defaultProtocol 
  setSocketOption sock KeepAlive <span style="color: red;">1</span> 
  connect sock <span style="color: green;">&#40;</span>addrAddress serveraddr<span style="color: green;">&#41;</span> 
  h <span style="color: #339933; font-weight: bold;">&lt;-</span> socketToHandle sock ReadWriteMode 
  hSetBuffering h <span style="color: green;">&#40;</span>BlockBuffering Nothing<span style="color: green;">&#41;</span> 
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a> h 
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- Ferme la connexion dont le Handle est donné </span>
closeConnection <span style="color: #339933; font-weight: bold;">::</span> Handle <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #cccc00; font-weight: bold;">IO</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span> 
closeConnection h <span style="color: #339933; font-weight: bold;">=</span> hClose h 
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- Envoie la chaîne donnée sur le Handle donné, et ensuite fait un flush sur ce handle, ce qui force l'envoi immédiat et nettoie les états </span>
sendMsg <span style="color: #339933; font-weight: bold;">::</span> Handle <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #cccc00; font-weight: bold;">IO</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span> 
sendMsg h msg <span style="color: #339933; font-weight: bold;">=</span> hPutStrLn h msg <span style="color: #339933; font-weight: bold;">&gt;&gt;</span> hFlush h 
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- Lis sur le Handle donné une chaîne et la renvoie (dans la monade IO) </span>
recvMsg <span style="color: #339933; font-weight: bold;">::</span> Handle <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #cccc00; font-weight: bold;">IO</span></a> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a> 
recvMsg h <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span> 
  msg <span style="color: #339933; font-weight: bold;">&lt;-</span> hGetContents h  
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a> msg</pre></td></tr></table></div>

<p>Voyons maintenant la fonction main.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p421code10'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p42110"><td class="code" id="p421code10"><pre class="haskell" style="font-family:monospace;">main <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span> 
  args <span style="color: #339933; font-weight: bold;">&lt;-</span> getArgs 
  <span style="color: #06c; font-weight: bold;">let</span> <span style="color: green;">&#40;</span>host<span style="color: #339933; font-weight: bold;">,</span> port<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>args <span style="color: #339933; font-weight: bold;">!!</span> <span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">,</span> args <span style="color: #339933; font-weight: bold;">!!</span> <span style="color: red;">1</span><span style="color: green;">&#41;</span></pre></td></tr></table></div>

<p>On récupère les arguments donnés au programme (s&#8217;il n&#8217;y en a pas assez, cela fera planter le logiciel &#8212; je ne me suis pas vraiment préoccupé de tous les cas d&#8217;erreur, mais ce n&#8217;était pas ma priorité ici) via la fonction getArgs, et l&#8217;on en récupère l&#8217;host et le port donnés.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p421code11'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p42111"><td class="code" id="p421code11"><pre class="haskell" style="font-family:monospace;">  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:putStrLn"><span style="font-weight: bold;">putStrLn</span></a> <span style="color: #339933; font-weight: bold;">$</span> <span style="background-color: #3cb371;">&quot;Opening client on &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> port <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot;<span style="background-color: #3cb371; font-weight: bold;">\n</span>&quot;</span> 
  hclient <span style="color: #339933; font-weight: bold;">&lt;-</span> openConnection host port 
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:putStrLn"><span style="font-weight: bold;">putStrLn</span></a> <span style="background-color: #3cb371;">&quot;Client connected&quot;</span></pre></td></tr></table></div>

<p>Ca y est, on se connecte, en affichant des messages pour tenir l&#8217;utilisateur informé. Si la connection échoue, le programme s&#8217;arrêtera avec une erreur &#8212; cf remarque ci-dessus.</p>
<p>Et maintenant, la boucle qui permet de lire depuis le réseau et lire depuis l&#8217;utilisateur, etc.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p421code12'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p42112"><td class="code" id="p421code12"><pre class="haskell" style="font-family:monospace;">  forever <span style="color: #339933; font-weight: bold;">$</span> <span style="color: #06c; font-weight: bold;">do</span> 
    <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:getLine"><span style="font-weight: bold;">getLine</span></a> <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> sendMsg hclient 
    forkIO <span style="color: #339933; font-weight: bold;">$</span> recvMsg hclient <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:putStrLn"><span style="font-weight: bold;">putStrLn</span></a></pre></td></tr></table></div>

<p>Ici, on a une sorte de boucle &laquo;&nbsp;à la while(true)&nbsp;&raquo;, dans laquelle on va tour à tour récupérer une entrée de l&#8217;utilisateur et l&#8217;envoyer au serveur, puis, dans un thread séparé, récupérer ce qu&#8217;a envoyé le serveur et l&#8217;afficher.<br />
Enfin, on ferme la connection (ce code est inutile tant que l&#8217;on a pas de possibilité de sortir du forever&#8230; qui devra tôt ou tard se transformer en &laquo;&nbsp;until&nbsp;&raquo; ou autre).</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p421code13'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p42113"><td class="code" id="p421code13"><pre class="haskell" style="font-family:monospace;">  closeConnection hclient</pre></td></tr></table></div>

<p>Voilà le code complet :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p421code14'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p42114"><td class="code" id="p421code14"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">import</span> Control<span style="color: #339933; font-weight: bold;">.</span>Concurrent 
<span style="color: #06c; font-weight: bold;">import</span> Control<span style="color: #339933; font-weight: bold;">.</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Monad"><span style="color: #cccc00; font-weight: bold;">Monad</span></a> 
<span style="color: #06c; font-weight: bold;">import</span> Network<span style="color: #339933; font-weight: bold;">.</span>Socket 
<span style="color: #06c; font-weight: bold;">import</span> Network<span style="color: #339933; font-weight: bold;">.</span>BSD 
<span style="color: #06c; font-weight: bold;">import</span> System<span style="color: #339933; font-weight: bold;">.</span>Environment 
<span style="color: #06c; font-weight: bold;">import</span> System<span style="color: #339933; font-weight: bold;">.</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #cccc00; font-weight: bold;">IO</span></a> 
&nbsp;
openConnection <span style="color: #339933; font-weight: bold;">::</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> ServiceName <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #cccc00; font-weight: bold;">IO</span></a> Handle 
openConnection hostname port <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span> 
  addrInfos <span style="color: #339933; font-weight: bold;">&lt;-</span> getAddrInfo Nothing <span style="color: green;">&#40;</span>Just hostname<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>Just port<span style="color: green;">&#41;</span> 
  <span style="color: #06c; font-weight: bold;">let</span> serveraddr <span style="color: #339933; font-weight: bold;">=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:head"><span style="font-weight: bold;">head</span></a> addrInfos 
  sock <span style="color: #339933; font-weight: bold;">&lt;-</span> socket <span style="color: green;">&#40;</span>addrFamily serveraddr<span style="color: green;">&#41;</span> Stream defaultProtocol 
  setSocketOption sock KeepAlive <span style="color: red;">1</span> 
  connect sock <span style="color: green;">&#40;</span>addrAddress serveraddr<span style="color: green;">&#41;</span> 
  h <span style="color: #339933; font-weight: bold;">&lt;-</span> socketToHandle sock ReadWriteMode 
  hSetBuffering h <span style="color: green;">&#40;</span>BlockBuffering Nothing<span style="color: green;">&#41;</span> 
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a> h 
&nbsp;
closeConnection <span style="color: #339933; font-weight: bold;">::</span> Handle <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #cccc00; font-weight: bold;">IO</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span> 
closeConnection h <span style="color: #339933; font-weight: bold;">=</span> hClose h 
&nbsp;
sendMsg <span style="color: #339933; font-weight: bold;">::</span> Handle <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #cccc00; font-weight: bold;">IO</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#41;</span> 
sendMsg h msg <span style="color: #339933; font-weight: bold;">=</span> hPutStrLn h msg <span style="color: #339933; font-weight: bold;">&gt;&gt;</span> hFlush h 
&nbsp;
recvMsg <span style="color: #339933; font-weight: bold;">::</span> Handle <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span style="color: #cccc00; font-weight: bold;">IO</span></a> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a> 
recvMsg h <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span> 
  msg <span style="color: #339933; font-weight: bold;">&lt;-</span> hGetContents h  
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a> msg 
&nbsp;
main <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span> 
  args <span style="color: #339933; font-weight: bold;">&lt;-</span> getArgs 
  <span style="color: #06c; font-weight: bold;">let</span> <span style="color: green;">&#40;</span>host<span style="color: #339933; font-weight: bold;">,</span> port<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>args <span style="color: #339933; font-weight: bold;">!!</span> <span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">,</span> args <span style="color: #339933; font-weight: bold;">!!</span> <span style="color: red;">1</span><span style="color: green;">&#41;</span> 
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:putStrLn"><span style="font-weight: bold;">putStrLn</span></a> <span style="color: #339933; font-weight: bold;">$</span> <span style="background-color: #3cb371;">&quot;Opening client on &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> port <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot;<span style="background-color: #3cb371; font-weight: bold;">\n</span>&quot;</span> 
  hclient <span style="color: #339933; font-weight: bold;">&lt;-</span> openConnection host port 
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:putStrLn"><span style="font-weight: bold;">putStrLn</span></a> <span style="background-color: #3cb371;">&quot;Client connected&quot;</span> 
  forever <span style="color: #339933; font-weight: bold;">$</span> <span style="color: #06c; font-weight: bold;">do</span> 
    <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:getLine"><span style="font-weight: bold;">getLine</span></a> <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> sendMsg hclient 
    forkIO <span style="color: #339933; font-weight: bold;">$</span> recvMsg hclient <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:putStrLn"><span style="font-weight: bold;">putStrLn</span></a> 
  closeConnection hclient</pre></td></tr></table></div>

<p>Compilation :</p>
<blockquote><p>ghc &#8211;make -o net net.hs</p></blockquote>
<p>Puis 2 exemples d&#8217;exécution&#8230;<br />
<code>
<pre>
$ ./net google.fr 80
Opening client on google.fr:80
Client connected
GET / HTTP/1.0
HTTP/1.0 302 Found
Location: http://www.google.fr/
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Set-Cookie: PREF=ID=30b094ad566b5532:TM=1251456529:LM=1251456529:S=94IXp5-SGiaYADWc; expires=Sun, 28-Aug-2011 10:48:49 GMT; path=/; domain=.google.com
Date: Fri, 28 Aug 2009 10:48:49 GMT
Server: gws
Content-Length: 218 

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.fr/">here</A>.
</BODY></HTML> 

^C
</pre>
<p></code><br />
(c&#8217;est moi qui ai tapé &laquo;&nbsp;GET / HTTP/1.0&#8243;)<br />
( qui vous dira que la page a été déplacée <img src='http://www.coder-studio.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  )</p>
<p>Et enfin, la surprise&#8230;</p>
<blockquote><p>$ ./net towel.blinkenlights.nl 23</p></blockquote>
<p>Qui, une version simplifiée d&#8217;un film très connu, vous permettra de voir, Jedi.</p>
<p>Enjoy !</p>
]]></content:encoded>
			<wfw:commentRss>http://www.coder-studio.com/blog/petit-clone-de-client-telnet-en-haskell/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
