Sockets
Read
read text data from a socket connection
Calling Sequence
Parameters
Description
Examples
Read(sid, timeout)
sid
-
valid open socket ID
timeout
(optional) non-negative integer; specify a timeout in seconds
The procedure Read is used to read textual data in the form of Maple strings from a socket connection.
The argument sid must be a valid socket ID for an open socket connection. An optional timeout argument can be passed, which is taken to be the number of seconds to wait before timing out the read operation.
When called, Read blocks indefinitely until data becomes available on the socket sid at which time the available data (up to a system dependent maximum number of bytes) is read. Data read from the socket is returned as a Maple string. If called with the timeout parameter and no data becomes available on the socket sid within the specified number of seconds, then the Read operation returns false.
Calls of the form Read( sid, 0 ) immediately return the value false.
Because Maple strings cannot contain null bytes, Read should be used only when you are sure that the data coming in over the network consists of plain ASCII text. (This is frequently the case.) If data may contain null bytes, then you should use Sockets[ReadBinary] instead.
The Read procedure obeys the persistent timeout protocol. (See Sockets/timeout.)
This example illustrates how data may appear to be lost if it contains null bytes.
with⁡Sockets:
sid≔Open⁡mantis,echo
0
data≔op⁡convert⁡Hi,bytes,0,op⁡convert⁡there,bytes
data≔72,105,0,116,104,101,114,101
datalen≔nops⁡data:
data≔Array⁡data,datatype=integer1
data≔721050116104101114101
WriteBinary⁡sid,data
8
Read⁡sid
Hi
a≔Array⁡1..datalen,datatype=integer1
a≔00000000
ReadBinary⁡a,sid
a
721050116104101114101
Close⁡sid
true
The underlying problem is that Maple strings cannot contain null bytes.
convert⁡data,list
72,105,0,116,104,101,114,101
convert⁡,bytes
See Also
socket definition
Sockets/timeout
Sockets[ReadBinary]
Sockets[ReadLine]
Download Help Document