Standard library
TCP
TCP listen, connect, read, and write.
Introduction
TCP listen, connect, read, and write. Import std/net/tcp to bind the module as tcp. Call free functions as tcp.name(...). Struct methods use a receiver value.
/ std/net/tcp
This page is the package reference for std/net/tcp. Private helpers used only by co-located tests are not listed.
Struct · conn
TCP connection shape. The shape is exported as tcp.conn. Methods on the receiver are listed next.
/ std/net/tcp
; shape: tcp.conn
$ conn = tcp.connect("127.0.0.1:8080")Parameters: No parameters. Type or shape export. Returns: Shape export used for literals and methods.
conn · read
Reads up to limit bytes from the connection. Empty bytes often mean EOF or a failed read depending on the peer. Call form: conn.read(limit).
/ std/net/tcp
$ data = conn.read(4096)
Parameters: limit: maximum bytes to read. Returns: Bytes read from the connection.
conn · write
Writes data to the connection. Call form: conn.write(data).
/ std/net/tcp
$ n = conn.write("hi\n")Parameters: data: bytes or string to send. Returns: Integer bytes written, or -1 on failure.
conn · close
Closes the connection and marks it closed. Call form: conn.close().
/ std/net/tcp
conn.close()
Parameters: No parameters. Returns: The connection receiver.
Struct · listener
TCP listener shape returned by tcp.listen. The shape is exported as tcp.listener. Methods on the receiver are listed next.
/ std/net/tcp
; shape: tcp.listener
$ listener = tcp.listen("127.0.0.1:0")Parameters: No parameters. Type or shape export. Returns: Shape export used for literals and methods.
listener · accept
Accepts the next inbound connection. A failed accept may return a conn with open false and handle 0. Call form: listener.accept().
/ std/net/tcp
$ conn = listener.accept()
Parameters: No parameters. Returns: conn product for the accepted peer.
listener · close
Closes the listener socket. Call form: listener.close().
/ std/net/tcp
listener.close()
Parameters: No parameters. Returns: The listener receiver.
Functions
Free functions on tcp. Each function has a short description, an example, then parameters and return shape.
listen
Binds a TCP listener on addr. Call form: tcp.listen(addr).
/ std/net/tcp
$ listener = tcp.listen("127.0.0.1:0")Parameters: addr: listen address string such as "127.0.0.1:0". Returns: Listener product. Check open or handle fields for bind failure.
connect
Connects to a TCP address. Call form: tcp.connect(addr).
/ std/net/tcp
$ conn = tcp.connect("127.0.0.1:8080")Parameters: addr: remote address string. Returns: Connection product. Check open or handle fields for connect failure.
accept
Accepts the next connection on a listener. Call form: tcp.accept(lis).
/ std/net/tcp
$ listener = tcp.listen("127.0.0.1:0")
$ conn = tcp.accept(listener)Parameters: listener: TCP listener value. Returns: Connection product for the accepted peer.
read
Reads up to a limit of bytes from a connection. Call form: tcp.read(c, limit).
/ std/net/tcp
$ data = tcp.read(conn, 4096)
Parameters: conn: TCP connection. limit: maximum bytes to read. Returns: Bytes read from the connection.
write
Writes bytes to a connection. Call form: tcp.write(c, data).
/ std/net/tcp
$ n = tcp.write(conn, "hi\n")
Parameters: conn: TCP connection. data: bytes or string to send. Returns: Integer bytes written, or -1 on failure.
close
Closes a TCP connection or listener resource. Call form: tcp.close(x).
/ std/net/tcp
tcp.close(conn)
Parameters: resource: connection or listener to close. Returns: None.