xur_

> module

std::net

TCP, UDP and Unix-domain sockets

use std::net; feature net native only

std::net provides blocking sockets with a text interface: data is written as strings and read back as strings, with invalid UTF-8 replaced by U+FFFD. Fallible operations return Result with the shared io::Error (see xur.std.io(7)).

Addresses are strings such as "127.0.0.1:8080" or "example.com:80". Binding to port 0 picks a free port.

> types

┌ type TcpListener

A TCP socket accepting connections.

# TcpListener::bind(address: str) -> Result<TcpListener, Error>

Listens for TCP connections on address.

# tcp_listener.accept() -> Result<(TcpStream, str), Error>

Waits for a connection and returns its stream and the peer's address.

# tcp_listener.local_addr() -> Result<str, Error>

The address the listener is bound to.

┌ type TcpStream

A TCP connection.

# TcpStream::connect(address: str) -> Result<TcpStream, Error>

Connects to a TCP server.

# tcp_stream.read(max: u32) -> Result<str, Error>

Reads what is available, up to max bytes, waiting for at least one. Returns an empty string at the end of the stream.

# tcp_stream.read_line() -> Result<str, Error>

Reads up to and including the next newline. Returns an empty string at the end of the stream.

# tcp_stream.shutdown() -> Result<(), Error>

Shuts down both directions of the connection.

# tcp_stream.write(text: str) -> Result<(), Error>

Writes all of text.

┌ type UdpSocket

A UDP socket.

# UdpSocket::bind(address: str) -> Result<UdpSocket, Error>

Opens a UDP socket bound to address.

# udp_socket.local_addr() -> Result<str, Error>

The address the socket is bound to.

# udp_socket.recv_from(max: u32) -> Result<(str, str), Error>

Waits for a datagram and returns its text and the sender's address. A datagram longer than max bytes is cut short.

# udp_socket.send_to(text: str, address: str) -> Result<usize, Error>

Sends text as one datagram to address and returns the number of bytes sent.

┌ type UnixListener

Unix-domain sockets exist only on Unix.

A Unix-domain socket accepting connections.

# UnixListener::bind(path: str) -> Result<UnixListener, Error>

Listens on a Unix-domain socket at path, which must not already exist.

# unix_listener.accept() -> Result<UnixStream, Error>

Waits for a connection and returns its stream.

┌ type UnixStream

A connection over a Unix-domain socket.

# UnixStream::connect(path: str) -> Result<UnixStream, Error>

Connects to the Unix-domain socket at path.

# unix_stream.read(max: u32) -> Result<str, Error>

The same as for TcpStream.

# unix_stream.read_line() -> Result<str, Error>

The same as for TcpStream.

# unix_stream.shutdown() -> Result<(), Error>

The same as for TcpStream.

# unix_stream.write(text: str) -> Result<(), Error>

The same as for TcpStream.

> examples

use std::io;
use std::net;
use std::thread;

fn main() -> Result<(), io::Error> {
    let listener = net::TcpListener::bind("127.0.0.1:0")?;
    let addr = listener.local_addr()?;
    let client = thread::spawn(|| {
        let stream = net::TcpStream::connect(addr).unwrap();
        stream.write("hello\n").unwrap();
    });
    let (stream, peer) = listener.accept()?;
    io::println("{} sent {}", peer, stream.read_line()?.trim());
    client.join();
    Ok(())
}