accept — Accept pending TCP client connections with MATLAB-compatible socket semantics.
accept(server) waits for a pending TCP connection on the socket created by tcpserver. When a client connects, it returns a client struct with MATLAB-compatible tcpclient-style metadata and an opaque handle used by networking builtins.
Syntax
client = accept(server)
client = accept(server, "Timeout", timeout)
client = accept(server, Name, Value, ...)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
server | Any | Yes | — | tcpserver handle struct returned by tcpserver. |
name | StringScalar | Yes | "Timeout" | Name token. Supported value: "Timeout". |
timeout | NumericScalar | Yes | — | Per-call accept timeout in seconds (non-negative, supports Inf). |
name_value_pairs | Any | Variadic | — | Name/Value option pairs (currently Timeout only). |
Returns
| Name | Type | Description |
|---|---|---|
client | Any | tcpclient handle struct representing the accepted connection. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:accept:InvalidTcpServer | Server argument is missing/invalid or references a stale tcpserver handle. | accept: invalid tcpserver handle |
RunMat:accept:Timeout | No incoming connection arrives before timeout elapses. | accept: timed out waiting for a client connection |
RunMat:accept:InvalidNameValue | Name/Value options are malformed, unsupported, or have invalid values. | accept: invalid name-value arguments |
RunMat:accept:InternalError | Internal lock/stream setup operation fails. | accept: internal error |
RunMat:accept:AcceptFailed | Underlying socket accept call fails for reasons other than timeout. | accept: failed to accept client |
How accept works
- The first argument must be the struct returned from
tcpserver. RunMat validates that the struct contains a__tcpserver_idfield and raisesRunMat:accept:InvalidTcpServerwhen the identifier is missing or no longer points to an active listener. - By default,
acceptuses the timeout configured when the server was created (Timeoutname-value pair ontcpserver). You can override it per call withaccept(server, "Timeout", seconds). The timeout must be non-negative; the builtin raisesRunMat:accept:InvalidNameValuewhen the value is NaN, negative, or non-scalar. - Successful calls return immediately with a struct whose fields mirror MATLAB’s
tcpclientproperties (Address,Port,NumBytesAvailable,BytesAvailableFcn,ByteOrder,Timeout,UserData, andConnected). The struct also contains hidden fields__tcpserver_idand__tcpclient_idso higher-level builtins can operate on the live socket. - If no client connects before the timeout expires, the builtin raises
RunMat:accept:Timeout. - When the underlying OS reports an accept failure (for example, because the socket closed), the builtin raises
RunMat:accept:AcceptFailedwith the platform error message. - Networking occurs on the host CPU. If the server struct or timeout value lives on the GPU, RunMat gathers it to the host automatically before waiting for connections.
Does RunMat run accept on the GPU?
accept does not involve the GPU. Any inputs that originate on the GPU are gathered before validation to make sure socket operations run on the host. The returned struct is always CPU-resident. No acceleration-provider hooks are required for this builtin, and future GPU-aware networking features will continue to gather metadata automatically while keeping sockets on the host.
GPU memory and residency
No. TCP sockets run on the host, and accept gathers any GPU-resident scalars or structs before waiting for a connection. Keeping metadata on the GPU offers no benefit, and the builtin always returns CPU-resident structs with identifiers that reference host networking resources.
Examples
Accepting a localhost client connection
srv = tcpserver("127.0.0.1", 0);
port = srv.ServerPort;
tcpclient("127.0.0.1", port);
client = accept(srv);
disp(client.Address)
disp(client.Port)Expected output:
127.0.0.1
55000 % varies per runOverriding the timeout for a single accept call
srv = tcpserver("0.0.0.0", 0, "Timeout", 10);
try
client = accept(srv, "Timeout", 0.25);
catch err
disp(err.identifier)
endExpected output:
RunMat:accept:TimeoutInspecting connection metadata after accepting a client
srv = tcpserver("::1", 45000);
tcpclient("::1", 45000);
client = accept(srv);
fprintf("Remote peer %s:%d\\n", client.Address, client.Port);
fprintf("Byte order: %s\\n", client.ByteOrder)Expected output:
Remote peer ::1:51432
Byte order: little-endianHandling multiple queued clients sequentially
srv = tcpserver("127.0.0.1", 47000);
tcpclient("127.0.0.1", 47000);
tcpclient("127.0.0.1", 47000);
client1 = accept(srv);
client2 = accept(srv);
fprintf("First connection from %s\\n", client1.Address);
fprintf("Second connection from %s\\n", client2.Address)Expected output:
First connection from 127.0.0.1
Second connection from 127.0.0.1Using the returned identifier with other networking builtins
srv = tcpserver("127.0.0.1", 52000);
tcpclient("127.0.0.1", 52000);
client = accept(srv);
clientId = client.__tcpclient_id;
fprintf("Opaque client identifier: %d\\n", clientId)Expected output:
Opaque client identifier: 42 % identifier value varies per runUsing accept with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how accept changes the result.
Run a small accept example, explain the result, then change one input and compare the output.
FAQ
What happens if the server struct is invalid or already closed?⌄
RunMat reports RunMat:accept:InvalidTcpServer. Ensure you pass the struct returned by tcpserver and that the server is still active.
Can I accept multiple clients with the same server?⌄
Yes. Call accept repeatedly; each successful call registers a new client and returns its own struct while keeping the listener active.
How do I change the timeout globally?⌄
Configure it when creating the server (tcpserver(..., "Timeout", seconds)). You can override it per call with accept(srv, "Timeout", value) if needed.
Does RunMat support IPv6 clients?⌄
Yes. The builtin accepts IPv4 or IPv6 clients and records their string representation in the returned struct’s Address field.
Is there a queue limit for pending connections?⌄
The OS backlog applies. If the queue is full, new clients may be refused before accept sees them. Increase the backlog with OS-level tuning if needed.
Can I use accept in parallel workers?⌄
Yes. The listener must exist in the worker where you call accept, just like MATLAB. Future high-level helpers will coordinate cross-worker sharing.
How do I close the accepted client?⌄
Use forthcoming networking close helpers (or invoke platform APIs directly). Dropping the struct does not close the socket automatically; RunMat networking builtins reference the opaque identifier.
What does NumBytesAvailable represent?⌄
It mirrors MATLAB: the number of bytes buffered and ready to read. Initially zero; reading functions update it as data arrives.
Does the builtin support TLS?⌄
Not yet. TLS will be layered on top of the same client identifier once RunMat’s TLS provider lands.
Related Io functions
Repl Fs
addpath · cd · copyfile · delete · dir · exist · fullfile · genpath · getenv · ls · mkdir · movefile · path · pwd · rmdir · rmpath · run · savepath · setenv · tempdir · tempname · uigetfile · uiputfile
Tabular
csvread · csvwrite · detectImportOptions · dlmread · dlmwrite · readmatrix · spreadsheetImportOptions · writecell · writematrix · xlsread
Filetext
fclose · feof · fgetl · fgets · fileread · filewrite · fopen · fprintf · fread · frewind · fwrite
Import
Json
Archive
Http
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how accept is executed, line by line, in Rust.
- View the source for accept in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
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.