emacs.d/clones/docs.racket-lang.org/reference/ports.html
2022-08-24 19:36:32 +02:00

40 lines
No EOL
20 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"/><meta name="viewport" content="width=device-width, initial-scale=0.8"/><title>13.1&nbsp;Ports</title><link rel="stylesheet" type="text/css" href="../scribble.css" title="default"/><link rel="stylesheet" type="text/css" href="extras.css" title="default"/><link rel="stylesheet" type="text/css" href="../racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-style.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../manual-racket.css" title="default"/><link rel="stylesheet" type="text/css" href="../doc-site.css" title="default"/><script type="text/javascript" src="../scribble-common.js"></script><script type="text/javascript" src="../manual-racket.js"></script><script type="text/javascript" src="../manual-racket.js"></script><script type="text/javascript" src="../doc-site.js"></script><script type="text/javascript" src="../local-redirect/local-redirect.js"></script><script type="text/javascript" src="../local-redirect/local-user-redirect.js"></script><!--[if IE 6]><style type="text/css">.SIEHidden { overflow: hidden; }</style><![endif]--></head><body id="doc-racket-lang-org"><div class="tocset"><div class="tocview"><div class="tocviewlist tocviewlisttopspace"><div class="tocviewtitle"><table cellspacing="0" cellpadding="0"><tr><td style="width: 1em;"><a href="javascript:void(0);" title="Expand/Collapse" class="tocviewtoggle" onclick="TocviewToggle(this,&quot;tocview_0&quot;);">&#9658;</a></td><td></td><td><a href="index.html" class="tocviewlink" data-pltdoc="x">The Racket Reference</a></td></tr></table></div><div class="tocviewsublisttop" style="display: none;" id="tocview_0"><table cellspacing="0" cellpadding="0"><tr><td align="right">1&nbsp;</td><td><a href="model.html" class="tocviewlink" data-pltdoc="x">Language Model</a></td></tr><tr><td align="right">2&nbsp;</td><td><a href="notation.html" class="tocviewlink" data-pltdoc="x">Notation for Documentation</a></td></tr><tr><td align="right">3&nbsp;</td><td><a href="syntax.html" class="tocviewlink" data-pltdoc="x">Syntactic Forms</a></td></tr><tr><td align="right">4&nbsp;</td><td><a href="data.html" class="tocviewlink" data-pltdoc="x">Datatypes</a></td></tr><tr><td align="right">5&nbsp;</td><td><a href="structures.html" class="tocviewlink" data-pltdoc="x">Structures</a></td></tr><tr><td align="right">6&nbsp;</td><td><a href="mzlib_class.html" class="tocviewlink" data-pltdoc="x">Classes and Objects</a></td></tr><tr><td align="right">7&nbsp;</td><td><a href="mzlib_unit.html" class="tocviewlink" data-pltdoc="x">Units</a></td></tr><tr><td align="right">8&nbsp;</td><td><a href="contracts.html" class="tocviewlink" data-pltdoc="x">Contracts</a></td></tr><tr><td align="right">9&nbsp;</td><td><a href="match.html" class="tocviewlink" data-pltdoc="x">Pattern Matching</a></td></tr><tr><td align="right">10&nbsp;</td><td><a href="control.html" class="tocviewlink" data-pltdoc="x">Control Flow</a></td></tr><tr><td align="right">11&nbsp;</td><td><a href="concurrency.html" class="tocviewlink" data-pltdoc="x">Concurrency and Parallelism</a></td></tr><tr><td align="right">12&nbsp;</td><td><a href="Macros.html" class="tocviewlink" data-pltdoc="x">Macros</a></td></tr><tr><td align="right">13&nbsp;</td><td><a href="input-and-output.html" class="tocviewselflink" data-pltdoc="x">Input and Output</a></td></tr><tr><td align="right">14&nbsp;</td><td><a href="security.html" class="tocviewlink" data-pltdoc="x">Reflection and Security</a></td></tr><tr><td align="right">15&nbsp;</td><td><a href="os.html" class="tocviewlink" data-pltdoc="x">Operating System</a></td></tr><tr><td align="right">16&nbsp;</td><td><a href="memory.html" class="tocviewlink" data-pltdoc="x">Memory Management</a></td></tr><tr><td align="right">17&nbsp;</td><td><a href="unsafe.html" class="tocviewlink" data-pltdoc="x">Unsafe Operations</a></td></tr><tr><td align="right">18&nbsp;</td><td><a href="running.html" class="tocviewlink" data-pltdoc="x">Running Racket</a></td></tr><tr><td align="right"></td><td><a href="doc-bibliography.html" class="tocviewlink" data-pltdoc="x">Bibliography</a></td></tr><tr><td align="right"></td><td><a href="doc-index.html" class="tocviewlink" data-pltdoc="x">Index</a></td></tr></table></div></div><div class="tocviewlist"><table cellspacing="0" cellpadding="0"><tr><td style="width: 1em;"><a href="javascript:void(0);" title="Expand/Collapse" class="tocviewtoggle" onclick="TocviewToggle(this,&quot;tocview_1&quot;);">&#9658;</a></td><td>13&nbsp;</td><td><a href="input-and-output.html" class="tocviewlink" data-pltdoc="x">Input and Output</a></td></tr></table><div class="tocviewsublist" style="display: none;" id="tocview_1"><table cellspacing="0" cellpadding="0"><tr><td align="right">13.1&nbsp;</td><td><a href="ports.html" class="tocviewselflink" data-pltdoc="x">Ports</a></td></tr><tr><td align="right">13.2&nbsp;</td><td><a href="Byte_and_String_Input.html" class="tocviewlink" data-pltdoc="x">Byte and String Input</a></td></tr><tr><td align="right">13.3&nbsp;</td><td><a href="Byte_and_String_Output.html" class="tocviewlink" data-pltdoc="x">Byte and String Output</a></td></tr><tr><td align="right">13.4&nbsp;</td><td><a href="Reading.html" class="tocviewlink" data-pltdoc="x">Reading</a></td></tr><tr><td align="right">13.5&nbsp;</td><td><a href="Writing.html" class="tocviewlink" data-pltdoc="x">Writing</a></td></tr><tr><td align="right">13.6&nbsp;</td><td><a href="pretty-print.html" class="tocviewlink" data-pltdoc="x">Pretty Printing</a></td></tr><tr><td align="right">13.7&nbsp;</td><td><a href="Reader_Extension.html" class="tocviewlink" data-pltdoc="x">Reader Extension</a></td></tr><tr><td align="right">13.8&nbsp;</td><td><a href="Printer_Extension.html" class="tocviewlink" data-pltdoc="x">Printer Extension</a></td></tr><tr><td align="right">13.9&nbsp;</td><td><a href="serialization.html" class="tocviewlink" data-pltdoc="x">Serialization</a></td></tr><tr><td align="right">13.10&nbsp;</td><td><a href="fasl.html" class="tocviewlink" data-pltdoc="x">Fast-<wbr></wbr>Load Serialization</a></td></tr><tr><td align="right">13.11&nbsp;</td><td><a href="sha.html" class="tocviewlink" data-pltdoc="x">Cryptographic Hashing</a></td></tr></table></div></div><div class="tocviewlist"><table cellspacing="0" cellpadding="0"><tr><td style="width: 1em;"><a href="javascript:void(0);" title="Expand/Collapse" class="tocviewtoggle" onclick="TocviewToggle(this,&quot;tocview_2&quot;);">&#9660;</a></td><td>13.1&nbsp;</td><td><a href="ports.html" class="tocviewselflink" data-pltdoc="x">Ports</a></td></tr></table><div class="tocviewsublistbottom" style="display: block;" id="tocview_2"><table cellspacing="0" cellpadding="0"><tr><td align="right">13.1.1&nbsp;</td><td><a href="encodings.html" class="tocviewlink" data-pltdoc="x">Encodings and Locales</a></td></tr><tr><td align="right">13.1.2&nbsp;</td><td><a href="port-ops.html" class="tocviewlink" data-pltdoc="x">Managing Ports</a></td></tr><tr><td align="right">13.1.3&nbsp;</td><td><a href="port-buffers.html" class="tocviewlink" data-pltdoc="x">Port Buffers and Positions</a></td></tr><tr><td align="right">13.1.4&nbsp;</td><td><a href="linecol.html" class="tocviewlink" data-pltdoc="x">Counting Positions, Lines, and Columns</a></td></tr><tr><td align="right">13.1.5&nbsp;</td><td><a href="file-ports.html" class="tocviewlink" data-pltdoc="x">File Ports</a></td></tr><tr><td align="right">13.1.6&nbsp;</td><td><a href="stringport.html" class="tocviewlink" data-pltdoc="x">String Ports</a></td></tr><tr><td align="right">13.1.7&nbsp;</td><td><a href="pipeports.html" class="tocviewlink" data-pltdoc="x">Pipes</a></td></tr><tr><td align="right">13.1.8&nbsp;</td><td><a href="portstructs.html" class="tocviewlink" data-pltdoc="x">Structures as Ports</a></td></tr><tr><td align="right">13.1.9&nbsp;</td><td><a href="customport.html" class="tocviewlink" data-pltdoc="x">Custom Ports</a></td></tr><tr><td align="right">13.1.10&nbsp;</td><td><a href="port-lib.html" class="tocviewlink" data-pltdoc="x">More Port Constructors, Procedures, and Events</a></td></tr></table></div></div></div></div><div class="maincolumn"><div class="main"><div class="navsettop"><span class="navleft"><form class="searchform"><input class="searchbox" id="searchbox" type="text" tabindex="1" placeholder="...search manuals..." title="Enter a search string to search the manuals" onkeypress="return DoSearchKey(event, this, &quot;8.6&quot;, &quot;../&quot;);"/></form>&nbsp;&nbsp;<a href="https://docs.racket-lang.org/index.html" title="up to the documentation top" data-pltdoc="x" onclick="return GotoPLTRoot(&quot;8.6&quot;);">top</a><span class="tocsettoggle">&nbsp;&nbsp;<a href="javascript:void(0);" title="show/hide table of contents" onclick="TocsetToggle();">contents</a></span></span><span class="navright">&nbsp;&nbsp;<a href="input-and-output.html" title="backward to &quot;13 Input and Output&quot;" data-pltdoc="x">&larr; prev</a>&nbsp;&nbsp;<a href="input-and-output.html" title="up to &quot;13 Input and Output&quot;" data-pltdoc="x">up</a>&nbsp;&nbsp;<a href="encodings.html" title="forward to &quot;13.1.1 Encodings and Locales&quot;" data-pltdoc="x">next &rarr;</a></span>&nbsp;</div><h4 x-source-module="(lib &quot;scribblings/reference/reference.scrbl&quot;)" x-source-pkg="racket-doc" x-part-tag="&quot;ports&quot;">13.1<tt>&nbsp;</tt><a name="(part._ports)"></a>Ports</h4><p><a name="(tech._port)"></a><span style="font-style: italic">Ports</span> produce and/or consume bytes. An <a name="(tech._input._port)"></a><span style="font-style: italic">input port</span>
produces bytes, while an <a name="(tech._output._port)"></a><span style="font-style: italic">output port</span> consumes bytes (and
some ports are both input ports and output ports). When an input port
is provided to a character-based operation, the bytes are decoded to a
character, and character-based output operations similarly encode the
character to bytes; see <a href="encodings.html" data-pltdoc="x">Encodings and Locales</a>. In addition to bytes and
characters encoded as bytes, some ports can produce and/or consume
arbitrary values as <a name="(tech._special)"></a><span style="font-style: italic">special</span> results.</p><p>When a port corresponds to a file, network connection, or some other
system resource, it must be explicitly closed via
<span class="RktSym"><a href="port-ops.html#%28def._%28%28quote._~23~25kernel%29._close-input-port%29%29" class="RktValLink" data-pltdoc="x">close-input-port</a></span> or <span class="RktSym"><a href="port-ops.html#%28def._%28%28quote._~23~25kernel%29._close-output-port%29%29" class="RktValLink" data-pltdoc="x">close-output-port</a></span> (or indirectly
via <span class="RktSym"><a href="custodians.html#%28def._%28%28quote._~23~25kernel%29._custodian-shutdown-all%29%29" class="RktValLink" data-pltdoc="x">custodian-shutdown-all</a></span>) to release low-level resources
associated with the port. For any kind of port, after it is closed,
attempting to read from or write to the port raises <span class="RktSym"><a href="exns.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._exn~3afail%29%29" class="RktValLink" data-pltdoc="x">exn:fail</a></span>.</p><p>Data produced by a <a href="ports.html#%28tech._input._port%29" class="techoutside" data-pltdoc="x"><span class="techinside">input port</span></a> can be read or <a name="(tech._peek)"></a><span style="font-style: italic">peeked</span>. When data is read, it is considered consumed and
removed from the port&rsquo;s stream. When data is <a href="ports.html#%28tech._peek%29" class="techoutside" data-pltdoc="x"><span class="techinside">peek</span></a>ed, it remains
in the port&rsquo;s stream to be returned again by the next read or
<a href="ports.html#%28tech._peek%29" class="techoutside" data-pltdoc="x"><span class="techinside">peek</span></a>. Previously peeked data can be <a name="(tech._commit)"></a><span style="font-style: italic">committed</span>, which causes the data to be removed from the
port as for a read in a way that can be synchronized with other
attempts to <a href="ports.html#%28tech._peek%29" class="techoutside" data-pltdoc="x"><span class="techinside">peek</span></a> or read through a <a href="sync.html#%28tech._synchronizable._event%29" class="techoutside" data-pltdoc="x"><span class="techinside">synchronizable
event</span></a>. Both read and <a href="ports.html#%28tech._peek%29" class="techoutside" data-pltdoc="x"><span class="techinside">peek</span></a> operations are normally blocking, in
the sense that the read or <a href="ports.html#%28tech._peek%29" class="techoutside" data-pltdoc="x"><span class="techinside">peek</span></a> operation does not complete
until data is available from the port; non-blocking variants of read
and <a href="ports.html#%28tech._peek%29" class="techoutside" data-pltdoc="x"><span class="techinside">peek</span></a> operations are also available.</p><p>The global variable <span class="RktSym"><a href="port-ops.html#%28def._%28%28quote._~23~25kernel%29._eof%29%29" class="RktValLink" data-pltdoc="x">eof</a></span> is bound to the end-of-file value,
and <span class="RktSym"><a href="port-ops.html#%28def._%28%28quote._~23~25kernel%29._eof-object~3f%29%29" class="RktValLink" data-pltdoc="x">eof-object?</a></span> returns <span class="RktVal">#t</span> only when applied to this
value. Reading from a port produces an end-of-file result when the
port has no more data, but some ports may also return end-of-file
mid-stream. For example, a port connected to a Unix terminal returns
an end-of-file when the user types control-D; if the user provides
more input, the port returns additional bytes after the end-of-file.</p><p>Every port has a name, as reported by <span class="RktSym"><a href="inspectors.html#%28def._%28%28quote._~23~25kernel%29._object-name%29%29" class="RktValLink" data-pltdoc="x">object-name</a></span>. The name
can be any value, and it is used mostly for error-reporting
purposes. The <span class="RktSym"><a href="Reading.html#%28def._%28%28quote._~23~25kernel%29._read-syntax%29%29" class="RktValLink" data-pltdoc="x">read-syntax</a></span> procedure uses the name of an input
port as the default source location for the <a href="syntax-model.html#%28tech._syntax._object%29" class="techoutside" data-pltdoc="x"><span class="techinside">syntax objects</span></a> that
it produces.</p><p>A port can be used as a <a href="sync.html#%28tech._synchronizable._event%29" class="techoutside" data-pltdoc="x"><span class="techinside">synchronizable event</span></a>. An input port is
<a href="sync.html#%28tech._ready._for._synchronization%29" class="techoutside" data-pltdoc="x"><span class="techinside">ready for synchronization</span></a> when <span class="RktSym"><a href="Byte_and_String_Input.html#%28def._%28%28quote._~23~25kernel%29._read-byte%29%29" class="RktValLink" data-pltdoc="x">read-byte</a></span> would not
block, and an output port is <a href="sync.html#%28tech._ready._for._synchronization%29" class="techoutside" data-pltdoc="x"><span class="techinside">ready for synchronization</span></a> when
<span class="RktSym"><a href="Byte_and_String_Output.html#%28def._%28%28quote._~23~25kernel%29._write-bytes-avail%29%29" class="RktValLink" data-pltdoc="x">write-bytes-avail</a></span> would not block or when the port contains
buffered characters and <span class="RktSym"><a href="Byte_and_String_Output.html#%28def._%28%28quote._~23~25kernel%29._write-bytes-avail%2A%29%29" class="RktValLink" data-pltdoc="x">write-bytes-avail*</a></span> can flush part of
the buffer (although <span class="RktSym"><a href="Byte_and_String_Output.html#%28def._%28%28quote._~23~25kernel%29._write-bytes-avail%29%29" class="RktValLink" data-pltdoc="x">write-bytes-avail</a></span> might block). A value
that can act as both an input port and an output port acts as an input
port for a <a href="sync.html#%28tech._synchronizable._event%29" class="techoutside" data-pltdoc="x"><span class="techinside">synchronizable event</span></a>. The <a href="sync.html#%28tech._synchronization._result%29" class="techoutside" data-pltdoc="x"><span class="techinside">synchronization result</span></a> of a port is the port itself.</p><table cellspacing="0" cellpadding="0"><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="encodings.html" class="toclink" data-pltdoc="x">13.1.1<span class="hspace">&nbsp;</span>Encodings and Locales</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="port-ops.html" class="toclink" data-pltdoc="x">13.1.2<span class="hspace">&nbsp;</span>Managing Ports</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="port-buffers.html" class="toclink" data-pltdoc="x">13.1.3<span class="hspace">&nbsp;</span>Port Buffers and Positions</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="linecol.html" class="toclink" data-pltdoc="x">13.1.4<span class="hspace">&nbsp;</span>Counting Positions, Lines, and Columns</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="file-ports.html" class="toclink" data-pltdoc="x">13.1.5<span class="hspace">&nbsp;</span>File Ports</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="stringport.html" class="toclink" data-pltdoc="x">13.1.6<span class="hspace">&nbsp;</span>String Ports</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="pipeports.html" class="toclink" data-pltdoc="x">13.1.7<span class="hspace">&nbsp;</span>Pipes</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="portstructs.html" class="toclink" data-pltdoc="x">13.1.8<span class="hspace">&nbsp;</span>Structures as Ports</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="customport.html" class="toclink" data-pltdoc="x">13.1.9<span class="hspace">&nbsp;</span>Custom Ports</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="port-lib.html" class="toclink" data-pltdoc="x">13.1.10<span class="hspace">&nbsp;</span>More Port Constructors, Procedures, and Events</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="port-lib.html#%28part._.Port_.String_and_.List_.Conversions%29" class="toclink" data-pltdoc="x">13.1.10.1<span class="hspace">&nbsp;</span>Port String and List Conversions</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="port-lib.html#%28part._.Creating_.Ports%29" class="toclink" data-pltdoc="x">13.1.10.2<span class="hspace">&nbsp;</span>Creating Ports</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="port-lib.html#%28part._.Port_.Events%29" class="toclink" data-pltdoc="x">13.1.10.3<span class="hspace">&nbsp;</span>Port Events</a></p></td></tr><tr><td><p><span class="hspace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><a href="port-lib.html#%28part._.Copying_.Streams%29" class="toclink" data-pltdoc="x">13.1.10.4<span class="hspace">&nbsp;</span>Copying Streams</a></p></td></tr></table><div class="navsetbottom"><span class="navleft"><form class="searchform"><input class="searchbox" id="searchbox" type="text" tabindex="1" placeholder="...search manuals..." title="Enter a search string to search the manuals" onkeypress="return DoSearchKey(event, this, &quot;8.6&quot;, &quot;../&quot;);"/></form>&nbsp;&nbsp;<a href="https://docs.racket-lang.org/index.html" title="up to the documentation top" data-pltdoc="x" onclick="return GotoPLTRoot(&quot;8.6&quot;);">top</a><span class="tocsettoggle">&nbsp;&nbsp;<a href="javascript:void(0);" title="show/hide table of contents" onclick="TocsetToggle();">contents</a></span></span><span class="navright">&nbsp;&nbsp;<a href="input-and-output.html" title="backward to &quot;13 Input and Output&quot;" data-pltdoc="x">&larr; prev</a>&nbsp;&nbsp;<a href="input-and-output.html" title="up to &quot;13 Input and Output&quot;" data-pltdoc="x">up</a>&nbsp;&nbsp;<a href="encodings.html" title="forward to &quot;13.1.1 Encodings and Locales&quot;" data-pltdoc="x">next &rarr;</a></span>&nbsp;</div></div></div><div id="contextindicator">&nbsp;</div></body></html>