Peek - Maple Help

Sockets

 Peek
 check for data on a socket

 Calling Sequence Peek(sid)

Parameters

 sid - valid open socket ID

Description

 • By default, a read operation on a socket will block until data becomes available. The procedure Peek enables you to test for the presence of data on a socket. This is useful for avoiding the use of timeouts on calls to Sockets[Read], Sockets[ReadLine], or Sockets[ReadBinary].
 • The procedure Peek never blocks; either the value false or a string is returned immediately.
 • When Peek is called, it "samples" the data available on the socket without reading it. A string containing the data "sample" is returned. The same data will be available to the next call to Sockets[Read], Sockets[ReadLine], or Sockets[ReadBinary]. If no data is available on the socket, then the value false is returned.

Examples

 > $\mathrm{with}\left(\mathrm{Sockets}\right):$
 > $\mathrm{sid}≔\mathrm{Open}\left("localhost","echo"\right):$
 > $\mathrm{Write}\left(\mathrm{sid},"Sample data"\right):$
 > $\mathrm{Peek}\left(\mathrm{sid}\right)$
 ${"Sample data"}$ (1)
 > $\mathrm{Read}\left(\mathrm{sid}\right)$
 ${"Sample data"}$ (2)
 > $\mathrm{Peek}\left(\mathrm{sid}\right)$
 ${\mathrm{false}}$ (3)
 > $\mathrm{Write}\left(\mathrm{sid},"Sample data"\right):$
 > $\mathbf{while}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathrm{Peek}\left(\mathrm{sid}\right)\ne \mathrm{false}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathrm{Read}\left(\mathrm{sid}\right)\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}$
 ${"Sample data"}$ (4)
 > $\mathrm{Close}\left(\mathrm{sid}\right)$
 ${\mathrm{true}}$ (5)