tcp
stableTCP client and server primitives — connect to remote hosts, send and receive data over TCP streams, and listen for incoming connections.
use plugin tcp::{TcpClient, TcpClient.connect_timeout, write, …} Functions (17)
- TcpClient Opens a synchronous TCP connection to host:port.
- TcpClient.connect_timeout Connects with a maximum wait time in milliseconds.
- write Sends a string over the connection and flushes the buffer.
- write_bytes Sends raw bytes (e.g.
- write_all Sends all bytes of data (string or bytes), retrying internally until every byte is delivered.
- read Reads up to max_bytes from the stream.
- read_line Reads until a newline character and returns the line without the trailing \n or \r\n.
- read_exact Reads exactly n bytes, blocking until all bytes arrive.
- read_until Reads bytes from the stream until the specified byte value is encountered.
- peek Reads up to max_bytes without consuming them from the stream — the next read will see the same data again.
- set_timeout_ms Sets both read and write timeouts to ms milliseconds.
- set_nodelay Enables or disables the TCP_NODELAY option (Nagle algorithm).
- set_keepalive Enables or disables TCP keepalive probes at the OS level to detect dead connections.
- shutdown Shuts down the read side, write side, or both sides of the connection.
- close Closes the TCP connection by shutting down both sides and releasing the socket.
- TcpListener Binds a TCP server socket to addr (e.g.
- accept Blocks until an incoming connection arrives, then returns a TcpClient handle for that connection.
Opens a synchronous TCP connection to host:port.
Opens a synchronous TCP connection to host:port. Returns a handle for subsequent read/write operations.
use plugin tcp::{TcpClient}
let conn = TcpClient("example.com", 80)
conn.write(conn, "GET / HTTP/1.0\r\nHost: example.com\r\n\r\n")
let response = conn.read_line(conn)
print(response)
conn.close(conn)
Connects with a maximum wait time in milliseconds.
Connects with a maximum wait time in milliseconds. Errors if the connection is not established within the timeout.
use plugin tcp::{TcpClient}
let conn = TcpClient.connect_timeout("192.168.1.1", 8080, 3000)
Sends a string over the connection and flushes the buffer.
Sends a string over the connection and flushes the buffer. Returns the number of bytes actually written.
use plugin tcp::{TcpClient}
let conn = TcpClient("127.0.0.1", 9000)
let n = conn.write(conn, "PING\r\n")
print("sent {n} bytes")
Sends raw bytes (e.g.
Sends raw bytes (e.g. from a read_entry_bytes call) over the connection.
use plugin tcp::{TcpClient}
let conn = TcpClient("127.0.0.1", 9000)
conn.write_bytes(conn, [0x48, 0x69])
Sends all bytes of data (string or bytes), retrying internally until every byte is delivered.
Sends all bytes of data (string or bytes), retrying internally until every byte is delivered. Does not return a count.
use plugin tcp::{TcpClient}
let conn = TcpClient("127.0.0.1", 9000)
conn.write_all(conn, "large payload that must arrive in full\n")
Reads up to max_bytes from the stream.
Reads up to max_bytes from the stream. May return fewer bytes if less data is available.
use plugin tcp::{TcpClient}
let conn = TcpClient("127.0.0.1", 9000)
conn.write(conn, "HELLO\n")
let data = conn.read(conn, 1024)
Reads until a newline character and returns the line without the trailing \n or \r\n.
Reads until a newline character and returns the line without the trailing \n or \r\n.
use plugin tcp::{TcpClient}
let conn = TcpClient("towel.blinkenlights.nl", 23)
let line = conn.read_line(conn)
print(line)
Reads exactly n bytes, blocking until all bytes arrive.
Reads exactly n bytes, blocking until all bytes arrive. Errors on EOF before n bytes.
use plugin tcp::{TcpClient}
let conn = TcpClient("127.0.0.1", 9000)
let header = conn.read_exact(conn, 4)
Reads bytes from the stream until the specified byte value is encountered.
Reads bytes from the stream until the specified byte value is encountered. The delimiter is included in the result.
use plugin tcp::{TcpClient}
let conn = TcpClient("127.0.0.1", 9000)
let line = conn.read_until(conn, 10)
Reads up to max_bytes without consuming them from the stream — the next read will see the same data again.
Reads up to max_bytes without consuming them from the stream — the next read will see the same data again.
use plugin tcp::{TcpClient}
let conn = TcpClient("127.0.0.1", 9000)
let preview = conn.peek(conn, 4)
Sets both read and write timeouts to ms milliseconds.
Sets both read and write timeouts to ms milliseconds. Pass 0 to disable the timeout.
use plugin tcp::{TcpClient}
let conn = TcpClient("127.0.0.1", 9000)
conn.set_timeout_ms(conn, 5000)
Enables or disables the TCP_NODELAY option (Nagle algorithm).
Enables or disables the TCP_NODELAY option (Nagle algorithm). Set to true for lower latency on small messages.
use plugin tcp::{TcpClient}
let conn = TcpClient("127.0.0.1", 9000)
conn.set_nodelay(conn, true)
Enables or disables TCP keepalive probes at the OS level to detect dead connections.
Enables or disables TCP keepalive probes at the OS level to detect dead connections.
use plugin tcp::{TcpClient}
let conn = TcpClient("127.0.0.1", 9000)
conn.set_keepalive(conn, true)
Shuts down the read side, write side, or both sides of the connection.
Shuts down the read side, write side, or both sides of the connection. how must be "read", "write", or "both".
use plugin tcp::{TcpClient}
let conn = TcpClient("127.0.0.1", 9000)
conn.write_all(conn, "BYE\n")
conn.shutdown(conn, "write")
Closes the TCP connection by shutting down both sides and releasing the socket.
Closes the TCP connection by shutting down both sides and releasing the socket.
use plugin tcp::{TcpClient}
let conn = TcpClient("127.0.0.1", 9000)
conn.write(conn, "QUIT\n")
conn.close(conn)
Binds a TCP server socket to addr (e.g.
Binds a TCP server socket to addr (e.g. "0.0.0.0:8080"). Use accept in a loop to handle connections.
use plugin tcp::{TcpListener}
let server = TcpListener("0.0.0.0:8080")
print("listening on {server.local_addr(server)}")
let client = server.accept(server)
let msg = client.read_line(client)
client.write(client, "OK\r\n")
client.close(client)
Blocks until an incoming connection arrives, then returns a TcpClient handle for that connection.
Blocks until an incoming connection arrives, then returns a TcpClient handle for that connection.
use plugin tcp::{TcpListener}
let server = TcpListener("127.0.0.1:9999")
let conn = server.accept(server)
let data = conn.read_line(conn)
conn.write(conn, "received: {data}\n")