RunMat
GitHub

read — Read bytes or typed values from a tcpclient connection with MATLAB-compatible behavior.

read(t) consumes data waiting on the TCP/IP client returned by tcpclient (or accept). It follows MATLAB behavior for timeout handling, byte-order decoding, and optional datatype conversion so networking code ports directly.

Syntax

data = read(client)
data = read(client, count)
data = read(client, count, datatype)

Inputs

NameTypeRequiredDefaultDescription
clientAnyYestcpclient handle struct.
countNumericScalarYesRequested element count.
datatypeStringScalarNo"uint8"Data type label (for example "uint8", "double", "char", "string").

Returns

NameTypeDescription
dataAnyAvailable bytes or requested typed payload from the TCP client.

Errors

IdentifierWhenMessage
RunMat:read:InvalidTcpClientClient handle is missing, malformed, invalid, or disconnected.read: invalid tcpclient handle
RunMat:read:InvalidInputArgument list shape is unsupported for read.read: invalid argument list
RunMat:read:NotConnectedClient has no active socket connection.read: tcpclient is disconnected

How read works

  • data = read(t) waits until at least one byte becomes available (subject to the client timeout) and then drains the socket buffer, returning a row vector of doubles whose values come from the received bytes. If the peer closes the connection without sending data, the result is an empty row vector.
  • data = read(t, count) blocks until exactly count values are available (again honouring Timeout). When the peer closes the socket before satisfying the request the builtin raises RunMat:read:ConnectionClosed.
  • data = read(t, count, datatype) interprets the values using the requested MATLAB datatype. Supported tokens mirror MATLAB: "uint8" (default), "int8", "uint16", "int16", "uint32", "int32", "uint64", "int64", "single", "double", "char", and "string". Numeric outputs are returned as doubles; "char" produces a MATLAB-style character row vector and "string" returns a scalar string.
  • Every call honours the client’s ByteOrder property when decoding multi-byte numbers. Little-endian is the default, but "big-endian" is respected for data written in network byte order.
  • The builtin gathers GPU-resident arguments automatically, executes the socket read on the CPU, and returns host values.
  • Errors are raised with MATLAB-compatible identifiers: invalid client structs trigger RunMat:read:InvalidTcpClient, timeouts emit RunMat:read:Timeout, and connection closures before a requested count is met raise RunMat:read:ConnectionClosed.

Does RunMat run read on the GPU?

Networking is a host-only subsystem. When a client struct or argument arrives from the GPU, RunMat gathers the value back to the CPU before reading from the socket. No acceleration-provider hooks participate in the operation, and the result is always a host value (double tensor, char array, or string). Future GPU providers continue to gather metadata automatically so networking remains CPU-bound.

Examples

Reading a fixed number of bytes from a TCP echo service

client = tcpclient("127.0.0.1", 50000);
write(client, uint8(1:6));
payload = read(client, 6)

Expected output:

payload =
     1     2     3     4     5     6

Reading ASCII text as characters

client = tcpclient("127.0.0.1", 50001);
write(client, "RunMat TCP");
chars = read(client, 10, "char")

Expected output:

chars =
    'RunMat TCP'

Reading doubles written in big-endian byte order

client = tcpclient("localhost", 50002, "ByteOrder", "big-endian");
write(client, swapbytes([1 2 3], "double"));
values = read(client, 3, "double")

Expected output:

values =
     1     2     3

Reading all available data without specifying a count

client = tcpclient("127.0.0.1", 50003);
write(client, uint8([10 20 30]));
burst = read(client)

Expected output:

burst =
    10    20    30

Handling read timeouts gracefully

client = tcpclient("example.com", 12345, "Timeout", 0.5);
try
    data = read(client, 64);
catch err
    disp(err.identifier)
end

Expected output:

RunMat:read:Timeout

Using read with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how read changes the result.

Run a small read example, explain the result, then change one input and compare the output.

FAQ

Does read modify the client struct?

No. The builtin interacts with the socket stored in RunMat’s internal registry. The visible struct returned from tcpclient is passed by value and is not mutated in place.

What happens when the remote host closes the connection?

If the peer closes the connection before the requested count is satisfied, read raises RunMat:read:ConnectionClosed. When no specific count is requested (read(t)), the builtin returns whatever data was available before the closure (possibly an empty vector).

Does read support infinite timeouts?

Yes. Setting t.Timeout = Inf (or passing "Timeout", inf when constructing the client) leaves the socket in blocking mode. The builtin waits indefinitely until enough data arrives or the peer closes the connection.

How are multibyte integers decoded?

RunMat honours the client’s ByteOrder property ("little-endian" or "big-endian"). For example, read(t, 4, "uint16") consumes eight bytes and interprets each pair in the configured byte order.

Can I read UTF-8 strings directly?

Use the "string" datatype. The builtin converts each received byte directly into a MATLAB string scalar assuming UTF-8 (non-UTF-8 sequences fall back to byte-wise decoding).

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how read is executed, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.

  • RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
  • Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.