network connections
not in the browser
Net opens connections on the native target, on .NET, and in WebAssembly under Node. A program in a browser can't open a network connection, so there every operation answers Error.UNSUPPORTED, and the examples on this page can't run in the browser: build them with ghul build --target llvm, or read their output beside each one.
Tcp.connect(host, port) looks host up and connects to the first of its addresses that answers. send_line sends a line, and receive_line waits for the next one:
sent hello, heard hello sent goodbye, heard goodbye
Tcp, Endpoint, Address and the other names on this page are in Net. Every operation that waits returns a Concurrency.Future of a Result. The program awaits the future, and ! unwraps the result. receive_line gives null once the other end has finished sending, which is why the example's while let loop ends. A line longer than the connection's max_line_length, 1 MiB unless the program sets another, answers Error.LINE_TOO_LONG instead, so a client that never ends its line can't fill the program's memory.
use closes a connection when the block that holds it ends, however it ends. In the example, let connection = use await Tcp.connect(...)! keeps the connection open until the program ends. In the servers below, the statement use connection closes each client's connection when the function serving it returns. Their listeners are held without use, because the program closes them itself, with close().
failures
A failure a program is expected to handle is a Net.Error: HOST_NOT_FOUND, REFUSED, UNREACHABLE, TIMED_OUT, ADDRESS_IN_USE, PERMISSION_DENIED, RESET, CLOSED, LINE_TOO_LONG, UNSUPPORTED or OTHER. A ! on a failed result throws. To handle a failure where it happens, test the result with if let, as the servers below do. A bug, such as starting a second receive on a connection while one is waiting, throws.
servers
Tcp.listen(port) listens on every interface, and Tcp.listen(Endpoint(Address.LOOPBACK, port)) on this machine only. Port 0 lets the system choose a free port, and the listener's endpoint says which one it chose. accept() waits for the next client to connect.
Concurrency.detach lets a future run with nothing waiting for it, so a server serves each client while it waits for the next. A failure in a detached future is written to standard error. Once the program closes the listener, accept answers Error.CLOSED, which is how this echo server stops:
A program ends when its last statement does, on every target. A detached future still waiting then is abandoned.
a chat server
A chat server keeps a list of the clients connected to it and sends each line one of them says to all the others. Where a send fails, the server drops that client:
The server and the clients that talk to it run on one thread: each await lets the others carry on while it waits.
bytes
A Connection reads into and writes from two IO.Bytes buffers, its input and output. receive() adds whatever has arrived to input, and receive_bytes(count) waits for exactly count bytes. An IO.Bytes is read from the front with take, take_line and take_text and written to the back with put and put_line. It never waits and never touches the network, so code that reads or writes a protocol over one can be tested without a connection.