OxL Reference

data TcpStream

A connected TCP stream. @must_close: end it with close() (use defer).

Example:

oxl✓ checked in CI
use std.net { SocketAddr, TcpListener, TcpStream, IpAddr }

fn main() -> () | NetError | ParseError  io, net {
  let l = TcpListener.bind(SocketAddr.parse("127.0.0.1:0")?)?
  defer l.close()?
  let c = TcpStream.connect(l.local, 2s)?
  defer c.close()?
  let s = l.accept(3s)?
  defer s.close()?
  c.writeText("ping")?
  print(s.readText(16)?)
}
Output
ping
@must_close
pub(api) data TcpStream

Thread safety. one task reads and one task writes at a time; do not share a stream between two readers.

Since. 1.0.0-beta

Fields

pub(api) local: SocketAddr

Address of this end.

pub(api) peer: SocketAddr

Address of the other end.

Methods

connect(addr: SocketAddr, timeout: Duration?) -> TcpStream | NetError net
Connects to addr (TCP_NODELAY is set).
connectHost(host: str, port: u16, timeout: Duration?) -> TcpStream | NetError net
Resolves host (a name or numeric address) and connects to the first address that accepts.
read(max: usize, timeout: Duration?) -> bytes | NetError net
Up to max bytes.
readText(max: usize, timeout: Duration?) -> str | NetError net
Like read, for UTF-8 text; bytes that are not valid UTF-8 give ProtocolError.
write(buf: bytes, timeout: Duration?) -> () | NetError net
Writes all of data.
writeText(text: str, timeout: Duration?) -> () | NetError net
Writes the UTF-8 bytes of text.
shutdownWrite() -> () | NetError net
Closes the sending side (the peer reads end of stream) and keeps receiving.
setNoDelay(on: bool) -> () | NetError net
Turns TCP_NODELAY (no coalescing of small writes) on or off; it is on after connect and accept.
close() -> () | NetError net
Closes the stream.

Defined in: std/net/net.oxl:461

See also