sys::tcp
use sys::io::{Read, Lines, Write, Close};
/// An opaque handle to a TCP listener. The listener is closed when
/// there are no remaining references to the TcpListener value.
type TcpListener;
/// A connected TCP stream.
type TcpStream;
/// A connected network socket. Implemented by [TcpStream] and by
/// `sys::tls::TlsStream`, since a TLS session is still a socket.
trait Socket {
/// Shutdown the write half of the stream.
val shutdown: fn(self) -> Result<null, `TCPError(string)>;
/// Get the remote address of the connected peer.
val peer_addr: fn(self) -> Result<string, `TCPError(string)>;
/// Get the local address of the stream.
val local_addr: fn(self) -> Result<string, `TCPError(string)>;
};
impl Read for TcpStream;
impl Lines for TcpStream;
impl Write for TcpStream;
impl Close for TcpStream;
impl Socket for TcpStream;
/// Connect to a TCP server at the given address (host:port).
val connect: fn(addr: string) -> Result<TcpStream, `TCPError(string)>;
/// Bind a TCP listener to the given address (host:port).
val listen: fn(addr: string) -> Result<TcpListener, `TCPError(string)>;
/// Accept a new connection from the listener. The second argument
/// is a trigger — each time it updates, a new accept is performed.
val accept: fn(listener: TcpListener, trigger: Any) -> Result<TcpStream, `TCPError(string)>;
/// Get the local address that the listener is bound to.
val listener_addr: fn(listener: TcpListener) -> Result<string, `TCPError(string)>;
TcpStream implements the sys::io traits, so reading and writing a
socket is the same code as reading and writing a file:
use sys::io::{Read, Write};
use sys::tcp::Socket;
let s = sys::tcp::connect("example.com:80")?;
Write::write_exact(s, buffer::from_string("GET / HTTP/1.0\r\n\r\n"))?;
let reply = buffer::to_string(Read::read_all(s)?)?;
Socket::peer_addr(s)?