Standard library
std/net
TCP networking.
TCP networking. Calls block only the calling task: while one waits for the network, others keep running on its thread.
let ln = try net.listen(":8080")
scope {
loop {
let c = try ln.accept()
go serve(c)
}
}Types: type Error, struct Listener, struct Conn
Types
type Error
pub type Error = io.ErrorErrors here are io.Error; isCancelled() tells a cancelled call apart.
struct Listener
pub struct Listener {
// and private fields
}A listening TCP socket, from listen. Closed when dropped.
Listener.accept
pub fn accept(self) -> Conn ! ErrorWaits for the next connection.
Listener.port
pub fn port(self) -> intThe port it listens on; useful after listen(":0").
struct Conn
pub struct Conn {
// and private fields
}A TCP connection, from dial or Listener.accept. Closed when dropped. It
is an io.Reader and an io.Writer.
Conn.read
pub fn read(self, buf: inout Vec[byte], max: int) -> int ! ErrorAppends up to max bytes to buf, waiting until at least one arrives.
Returns how many were read; 0 means the peer closed the connection.
Conn.write
pub fn write(self, data: []byte) ! ErrorWrites all of data.
Conn.writeText
pub fn writeText(self, s: str) ! ErrorWrites the bytes of s.
Conn.readAll
pub fn readAll(self) -> Vec[byte] ! ErrorReads until the peer closes the connection.
Conn.closeWrite
pub fn closeWrite(self)Tells the peer no more data is coming; reading still works.
Functions
listen
pub fn listen(addr: str) -> Listener ! ErrorListens on "host:port"; ":port" listens on all interfaces, ":0" picks a free port.
dial
pub fn dial(addr: str) -> Conn ! ErrorConnects to addr ("host:port"; the host may be a name, which is looked up).