Module std::net

TCP networking.

Create TCP servers and clients for bidirectional byte-stream communication. Handle types (Listener, Connection) provide type-safe wrappers over raw file descriptors.

Examples

import std::net;

fn main() {
    let listener = net.listen(":9000");
    let conn = listener.accept();
    let data = conn.read();
    conn.write(data);
    conn.close();
}

Contents

Functions

Function listen

pub fn listen(addr: String) -> Listener

Create a TCP listener bound to the given address.

The address format is "host:port" or ":port".

Examples

let listener = net.listen(":9000");

Function connect

pub fn connect(addr: String) -> Connection

Connect to a TCP server at the given address.

Blocks until the connection is established.

Examples

let conn = net.connect("localhost:9000");

Function connect_timeout

pub fn connect_timeout(addr: String, timeout_sec: i32, timeout_usec: i32) -> Connection

Connect to a TCP server with a timeout.

timeout_sec and timeout_usec specify the deadline.

Function broadcast_except

pub fn broadcast_except(sender: Connection, message: bytes) -> i32

Broadcast a message to all connections except the sender.

Used in chat-server patterns to fan out messages.

Types

Struct Listener

A TCP listener bound to an address, waiting for connections.

Created by net.listen(addr). Returns a negative value (as i32) on failure when cast; use comparison to check success.

Struct Connection

An established TCP connection for reading and writing.

Obtained from listener.accept() or net.connect(addr).

Traits

Trait ListenerMethods

Methods available on a TCP Listener.

Methods

fn accept(self: Listener) -> Connection

Trait ConnectionMethods

Methods available on a TCP Connection.

Methods

fn read(self: Connection) -> bytes fn read_string(self: Connection) -> String fn write(self: Connection, data: bytes) fn write_string(self: Connection, data: String) fn close(self: Connection) -> i32 fn set_read_timeout(self: Connection, ms: i32) -> i32 fn set_write_timeout(self: Connection, ms: i32) -> i32