Skip to content
FutureBlitz

    Install

    Import std.net.tcp

    TcpStream

    std.net.tcp

    class

    Public Class TcpStream Implements Reader, Writer

    A TCP connection: bytes both ways, in order, which it reads and writes as std.io's streams do. Nagle's algorithm is off, so a small write goes at once, as interactive messages need. A Read or Write that fails throws an IOError whose Cause is a NetError, of RESET for a connection the other side ended badly, or a TimeoutError. It closes when it goes, or at Close.

    Examples

    Local stream = Try TcpStream.Connect("example.com", 80)
    Try stream.WriteText("HEAD / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n")
    Print(Try ReadAllText(stream))
    stream.Close()

    std/net/tcp.fb, line 34

    • Public Static Method Connect(address: SocketAddress, timeout: Duration? = Null): TcpStream Throws NetError, TimeoutError static method #

      Connects to address, waiting up to timeout, or as long as the OS does for a Null one. Throws a NetError of REFUSED when nothing listens there, of UNREACHABLE when the network can't get there, and a TimeoutError when it takes too long.

    • Public Static Method Connect(host: String, port: UInt16, timeout: Duration? = Null): TcpStream Throws NetError, TimeoutError static method #

      Connects to host at port, trying its addresses in the order Dns.Resolve gives them, each for up to timeout, and throws what the last one did when none connects.

    • Public Static Async Method ConnectAsync(address: SocketAddress, timeout: Duration? = Null): TcpStream Throws NetError, TimeoutError static method #

      Connect's Async twin, which waits on the main thread.

    • Public Static Async Method ConnectAsync(host: String, port: UInt16, timeout: Duration? = Null): TcpStream Throws NetError, TimeoutError static method #

      Connect's Async twin to a host, through Dns.ResolveAsync.

    • Public Property LocalAddress: SocketAddress property #

      Its own address.

    • Public Property RemoteAddress: SocketAddress property #

      The other side's address.

    • Public Property ReadTimeout: Duration? property #

      How long a Read or ReadAsync waits for bytes before it throws, or Null, as it starts, to wait as long as it takes.

    • Public Property IsClosed: Bool property #

      Whether Close closed it.

    • Public Method Read(into: Buffer<UInt8>): Int64 Throws IOError method #

      Reads what's come, at least a byte, waiting for some, or 0 once the other side ended what it writes.

    • Public Async Method ReadAsync(into: Buffer<UInt8>): Int64 Throws IOError method #

      Read's Async twin, which waits on the I/O thread for the connection's bytes while the main thread goes on.

    • Public Method Write(from: Buffer<UInt8>) Throws IOError method #

      Writes all of from's bytes, returning once the OS has them.

    • Public Async Method WriteAsync(from: Buffer<UInt8>) Throws IOError method #

      Write's Async twin, which waits on the I/O thread while the connection's bytes go, while the main thread goes on.

    • Public Method ShutdownWrite() Throws IOError method #

      Ends what it writes, once what's written is sent, so the other side reads its end; it still reads what the other side writes.

    • Public Async Method ShutdownWriteAsync() Throws IOError method #

      What ShutdownWrite does, without blocking its thread: a call gives its Task.

    • Public Method Close() method #

      Closes it at once: a Read or Write waiting for it throws an IOError whose Cause is a NetError of CLOSED, and so does one after. Going closes it too.

    Report an issue with this page or with FutureBlitz on GitHub.