Import std.net.tcp
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 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()-
Public Static Method Connect(address: SocketAddress, timeout: Duration? = Null): TcpStream Throws NetError, TimeoutErrorstatic method #Connects to
address, waiting up totimeout, 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, TimeoutErrorstatic method #Connects to
hostatport, trying its addresses in the order Dns.Resolve gives them, each for up totimeout, and throws what the last one did when none connects. -
Public Static Async Method ConnectAsync(address: SocketAddress, timeout: Duration? = Null): TcpStream Throws NetError, TimeoutErrorstatic 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, TimeoutErrorstatic method #Connect's Async twin to a host, through Dns.ResolveAsync.
-
Its own address.
-
The other side's address.
-
How long a Read or ReadAsync waits for bytes before it throws, or Null, as it starts, to wait as long as it takes.
-
Whether Close closed it.
-
Reads what's come, at least a byte, waiting for some, or 0 once the other side ended what it writes.
-
Read's Async twin, which waits on the I/O thread for the connection's bytes while the main thread goes on.
-
Writes all of from's bytes, returning once the OS has them.
-
Write's Async twin, which waits on the I/O thread while the connection's bytes go, while the main thread goes on.
-
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.
-
What ShutdownWrite does, without blocking its thread: a call gives its Task.
-
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.