<html><head><metahttp-equiv="content-type"content="text/html; charset=utf-8"/><title>6 Examples</title><linkrel="stylesheet"type="text/css"href="scribble.css"title="default"/><linkrel="stylesheet"type="text/css"href="racket.css"title="default"/><linkrel="stylesheet"type="text/css"href="scribble-style.css"title="default"/><scripttype="text/javascript"src="scribble-common.js"></script><!--[if IE 6]><style type="text/css">.SIEHidden { overflow: hidden; }</style><![endif]--></head><bodyid="scribble-racket-lang-org"><divclass="tocset"><divclass="tocview"><divclass="tocviewlist"style="margin-bottom: 1em;"><divclass="tocviewtitle"><tablecellspacing="0"cellpadding="0"><tr><tdstyle="width: 1em;"><ahref="javascript:void(0);"title="Expand/Collapse"class="tocviewtoggle"onclick="TocviewToggle(this,"tocview_0");">▼</a></td><td></td><td><ahref="index.html"class="tocviewlink"data-pltdoc="x">Marketplace:<spanclass="mywbr"> </span> Network-<wbr></wbr>Aware Programming</a></td></tr></table></div><divclass="tocviewsublisttop"style="display: block;"id="tocview_0"><tablecellspacing="0"cellpadding="0"><tr><tdalign="right">1 </td><td><ahref="Concepts.html"class="tocviewlink"data-pltdoc="x">Concepts</a></td></tr><tr><tdalign="right">2 </td><td><ahref="high-level-interface.html"class="tocviewlink"data-pltdoc="x">High-<wbr></wbr>level interface</a></td></tr><tr><tdalign="right">3 </td><td><ahref="low-level-interface.html"class="tocviewlink"data-pltdoc="x">Low-<wbr></wbr>level interface</a></td></tr><tr><tdalign="right">4 </td><td><ahref="Drivers.html"class="tocviewlink"data-pltdoc="x">Drivers</a></td></tr><tr><tdalign="right">5 </td><td><ahref="Management_and_Monitoring.html"class="tocviewlink"data-pltdoc="x">Management and Monitoring</a></td></tr><tr><tdalign="right">6 </td><td><ahref=""class="tocviewselflink"data-pltdoc="x">Examples</a></td></tr></table></div></div><divclass="tocviewlist"><tablecellspacing="0"cellpadding="0"><tr><tdstyle="width: 1em;"><ahref="javascript:void(0);"title="Expand/Collapse"class="tocviewtoggle"onclick="TocviewToggle(this,"tocview_1");">►</a></td><td>6 </td><td><ahref=""class="tocviewselflink"data-pltdoc="x">Examples</a></td></tr></table><divclass="tocviewsublistbottom"style="display: none;"id="tocview_1"><tablecellspacing="0"cellpadding="0"><tr><tdalign="right">6.1 </td><td><ahref="#%28part._echo-server-example%29"class="tocviewlink"data-pltdoc="x">TCP echo server</a></td></tr><tr><tdalign="right">6.2 </td><td><ahref="#%28part._chat-server-example%29"class="tocviewlink"data-pltdoc="x">TCP chat server</a></td></tr><tr><tdalign="right">6.3 </td><td><ahref="#%28part._chat-client-example%29"class="tocviewlink"data-pltdoc="x">TCP chat client</a></td></tr></table></div></div></div><divclass="tocsub"><divclass="tocsubtitle">On this page:</div><tableclass="tocsublist"cellspacing="0"><tr><td><spanclass="tocsublinknumber">6.1<tt> </tt></span><ahref="#%28part._echo-server-example%29"class="tocsubseclink"data-pltdoc="x">TCP echo server</a></td></tr><tr><td><spanclass="tocsublinknumber">6.2<tt> </tt></span><ahref="#%28part._chat-server-example%29"class="tocsubseclink"data-pltdoc="x">TCP chat server</a></td></tr><tr><td><spanclass="tocsublinknumber">6.3<tt> </tt></span><ahref="#%28part._chat-client-example%29"class="tocsubseclink"data-pltdoc="x">TCP chat client</a></td></tr></table></div></div><divclass="maincolumn"><divclass="main"><divclass="versionbox"><spanclass="version">5.3.4.11</span></div><divclass="navsettop"><spanclass="navleft"> </span><spanclass="navright"> <ahref="Management_and_Monitoring.html"title="backward to "5 Management and Monitoring""data-pltdoc="x">← prev</a> <ahref="index.html"title="up to "Marketplace: Network-Aware Programming""data-pltdoc="x">up</a> <spanclass="nonavigation">next →</span></span> </
arriving on port 5999 and <spanclass="RktSym"><ahref="high-level-interface.html#%28form._%28%28lib._marketplace%2Fsugar-untyped..rkt%29._spawn%29%29"class="RktStxLink"data-pltdoc="x">spawn</a></span>s a fresh process in response
to each with the help of the auxiliary <spanclass="RktSym">echoer</span> function. The
topic of conversation associated with the each new connection is
parsed (with <spanclass="RktSym"><ahref="high-level-interface.html#%28form._%28%28lib._marketplace%2Fsugar-untyped..rkt%29._match-conversation%29%29"class="RktStxLink"data-pltdoc="x">match-conversation</a></span>) to name the remote
passed to <spanclass="RktSym">echoer</span> to create the initial state and actions for
the corresponding process. In this case, the process is stateless,
indicated by the special constant <spanclass="RktSym">stateless</span>.</p><p>Each connection’s process watches for incoming data, using
<spanclass="RktSym">from</span> and <spanclass="RktSym">to</span> to configure a <spanclass="RktSym"><ahref="high-level-interface.html#%28form._%28%28lib._marketplace%2Fsugar-untyped..rkt%29._subscriber%29%29"class="RktStxLink"data-pltdoc="x">subscriber</a></span>. It
also declares its intent to produce outbound TCP data, using
<spanclass="RktSym"><ahref="high-level-interface.html#%28form._%28%28lib._marketplace%2Fsugar-untyped..rkt%29._publisher%29%29"class="RktStxLink"data-pltdoc="x">publisher</a></span>. When data arrives, it is echoed back to the remote
peer using the <spanclass="RktSym"><ahref="high-level-interface.html#%28def._%28%28lib._marketplace%2Fsugar-values..rkt%29._send-message%29%29"class="RktValLink"data-pltdoc="x">send-message</a></span> operation. Absence notifications
signal disconnection; when the remote peer closes the TCP connection,
the <spanclass="RktSym"><ahref="high-level-interface.html#%28form._%28%28lib._marketplace%2Fsugar-untyped..rkt%29._on-absence%29%29"class="RktStxLink"data-pltdoc="x">on-absence</a></span> handler issues a <spanclass="RktSym"><ahref="high-level-interface.html#%28def._%28%28lib._marketplace%2Fsugar-values..rkt%29._quit%29%29"class="RktValLink"data-pltdoc="x">quit</a></span> action, which
terminates the connection’s process.</p><h4>6.2<tt> </tt><aname="(part._chat-server-example)"></a>TCP chat server</h4><blockquoteclass="SCodeFlow"><blockquoteclass="Rfilebox"><pclass="Rfiletitle"><spanclass="Rfilename"><spanclass="stt">"examples/chat-paper.rkt"</span></span></p><blockquoteclass="Rfilecontent"><tablecellspacing="0"class="RktBlk"><tr><td><ahref="http://docs.racket-lang.org/guide/Module_Syntax.html#%28part._hash-lang%29"class="RktModLink"data-pltdoc="x"><spanclass="RktMod">#lang</span></a><spanclass="hspace"> </span><ahref="low-level-interface.html"class="RktModLink"data-pltdoc="x"><spanclass="RktSym">marketplace</span></a></td></tr><tr><td><spanclass="hspace"> </span></td></tr><tr><td><spanclass="RktPn">(</span><spanclass="RktSym"><ahref="high-level-interface.html#%28form._%28%28lib._marketplace%2Fsugar-untyped..rkt%29._spawn-vm%29%29"class="RktStxLink"data-pltdoc="x">spawn-vm</a></span></td></tr><tr><td><spanclass="hspace"> </span><spanclass="RktPn">(</span><spanclass="RktSym"><ahref="high-level-interface.html#%28def._%28%28lib._marketplace%2Fsugar-untyped..rkt%29._at-meta-level%29%29"class="RktValLink"data-pltdoc="x">at-meta-level</a></span></td></tr><tr><td><spanclass="hspace"> </span><spanclass="RktPn">(</span><spanclass="RktSym"><ahref="high-level-interface.html#%28form._%28%28lib._marketplace%2Fsugar-untyped..rkt%29._observe-publishers%29%29"class="RktStxLink"data-pltdoc="x">observe-publishers</a></span><spanclass="hspace"> </span><spanclass="RktPn">(</span><spanclass="RktSym">tcp-channel</span><spanclass="hspace"> </span><spanclass="RktSym"><ahref="low-level-interface.html#%28def._%28%28lib._marketplace%2Fsugar-untyped..rkt%29._~3f%29%29"class="RktValLink"data-pltdoc="x">?</a></span><spanclass="hspace"> </span><spanclass="RktPn">(</span><spanclass="RktSym">tcp-listener</span><spanclass="hspace"> </span><spanclass="RktVal">5999</span><spanclass="RktPn">)</span><spanclass="hspace"> </span><spanclass="RktSym"><ahref="low-level-interface.html#%28def._%28%28lib._marketplace%2Fsugar-untyped..rkt%29._~3f%29%29"class="RktValLink"data-pltdoc="x">?</a></span><spanclass="RktPn">)</span></td></tr><tr><td><spanclass="RktPn">(</span><spanclass="RktSym"><ahref="high-level-interface.html#%28form._%28%28lib._marketplace%2Fsugar-untyped..rkt%29._match-conversation%29%29"class="RktStxLink"data-pltdoc="x">match-conversation</a></span><spanclass="hspace"> </span><spanclass="RktPn">(</span><spanclass="RktSym">tcp-channel</span><spanclass="hspace"> </span><spanclass="RktSym">them</span><spanclass="hspace"> </span><spanclass="RktSym">us</span><spanclass="hspace"> </span><spanclass="RktSym"><ahref="http://docs.racket-lang.org/reference/stx-patterns.html#%28form._%28%28lib._racket%2Fprivate%2Fstxcase-scheme..rkt%29.__%29%29"class="RktStxLink"data-pltdoc="x">_</a></span><spanclass="RktPn">)</span></td></tr><tr><td><spanclass="RktPn">(</span><spanclass="RktSym"><ahref="high-level-interface.html#%28form._%28%28lib._marketplace%2Fsugar-untyped..rkt%29._on-presence%29%29"class="RktStxLink"data-pltdoc="x">on-presence</a></span><spanclass="hspace"> </span><spanclass="RktPn">(</span><spanclass="RktSym"><ahref="high-level-interface.html#%28form._%28%28lib._marketplace%2Fsugar-untyped..rkt%29._spawn%29%29"class="RktStxLink"data-pltdoc="x">spawn</a></span><spanclass="hspace"> </span><spanclass="RktPn">(</span><spanclass="RktSym">chat-session</span><spanclass="hspace"> </span><spanclass="RktSym">them</span><spanclass="hspace"> </span><spanclass="RktSym">us</span><spanclass="RktPn">)</span><spanclass="RktPn">)</span><spanclass="RktPn">)</span><spanclass="RktPn">)</span><spanclass="RktPn">)</span><spanclass="RktPn">)</span><spanclass="RktPn">)</span></td></tr><tr><td><spanclass="hspace"> </span></td></tr><tr><td><spanclass="RktPn">(</span><spanclass="RktSym"><ahref="http://docs.racket-lang.org/ts-reference/special-forms.html#%28for