Module net

TCP networking.

Create TCP servers and clients for bidirectional byte-stream communication.

Examples

import std::net;

fn main() {
    let listener = net.listen(":9000");
    let conn = listener.accept();
    let msg = conn.read();
    conn.write("echo: " + msg);
    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: string) -> 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).

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) -> string fn write(self: Connection, data: string) -> i32 fn close(self: Connection) -> i32 fn set_read_timeout(self: Connection, ms: i32) -> i32 fn set_write_timeout(self: Connection, ms: i32) -> i32