Skip to content
FutureBlitz

    Install

    Import std.net.tcp

    std.net.tcp

    std.net's TCP: TcpListener, which takes connections at an address, and TcpStream, a connection's bytes both ways, a Reader and a Writer as std.io's streams are, each blocking or Async.

    std/net/tcp.fb

    Types

    TcpStream 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 a game's 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.

    std/net/tcp.fb, line 22

    • 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 main thread instead of blocking it. One that can only block reads with Read.

      Doc from std.io.streams.Reader.ReadAsync.

    • 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 main thread instead of blocking it.

      Doc from std.io.streams.Writer.WriteAsync.

    • 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.

    TcpListener class

    Public Class TcpListener

    Takes TCP connections at an address, which Accept gives as TcpStreams. It closes when it goes, or at Close, with the connections it has that nothing accepted.

    std/net/tcp.fb, line 150

    • @PanicsAtCaller Public Static Method Listen(address: SocketAddress, backlog: Int = BACKLOG): TcpListener Throws NetError static method #

      Listens at address, whose port 0 has the OS pick one, which LocalAddress tells, keeping up to backlog connections the OS has that aren't taken yet, 1 at least, or it panics. Throws a NetError when it can't, as when something listens there already.

    • @PanicsAtCaller Public Static Method Listen(host: String, port: UInt16, backlog: Int = BACKLOG): TcpListener Throws NetError static method #

      Listens at host's first address, as Dns.Resolve gives them, and port.

    • Public Property LocalAddress: SocketAddress property #

      Where it listens, the port the OS picked included.

    • Public Property IsClosed: Bool property #

      Whether Close closed it.

    • Public Method Accept(timeout: Duration? = Null): TcpStream Throws NetError, TimeoutError method #

      The next connection, waiting up to timeout for one, or as long as it takes for a Null one, and throwing a TimeoutError after.

    • Public Async Method AcceptAsync(timeout: Duration? = Null): TcpStream Throws NetError, TimeoutError method #

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

    • Public Method Close() method #

      Closes it at once, with the connections nothing accepted: what's waiting for it throws a NetError of CLOSED, and so does what uses it after. Going closes it too.

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