Skip to content

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:

ghul
let connection = use await Tcp.connect("localhost", port)!
for line in ["hello", "goodbye"] do
await connection.send_line(line)!
write_line("sent {line}, heard {await connection.receive_line()!}")
od
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:

ghul
echo(connection: Tcp.Connection) -> Concurrency.Future is
use connection
while let line = await connection.receive_line()! do
await connection.send_line(line)!
od
si
// serves each client while it waits for the next, until the listener closes
serve(listener: Tcp.Listener) -> Concurrency.Future is
do
let accepted = await listener.accept()
if let failed: Result.ERROR[Tcp.Connection, Error] = ►accepted then
if !(failed.error =~ Error.CLOSED) then
write_line("accept failed: {failed.error}")
fi
return
fi
Concurrency.detach(echo(accepted!))
od
si
let listener = await Tcp.listen(Endpoint(Address.LOOPBACK, 0))!
let serving = serve(listener)

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:

ghul
class Member(name: string public, connection: Tcp.Connection public)
let members = LIST[Member]()
// to everyone but the sender, dropping a member whose send fails
broadcast(sender: Member, text: string) -> Concurrency.Future is
for member in members |> filter(m => m != sender) |> list() do
let sent = await member.connection.send_line(text)
if !►sent? then
members.remove(member)
fi
od
si
attend(connection: Tcp.Connection) -> Concurrency.Future is
use connection
await connection.send_line("your name?")!
if let name = await connection.receive_line()! then
let member = Member(name, connection)
members.add(member)
await connection.send_line("welcome, {name}")!
await broadcast(member, "{name} joined")
while let line = await connection.receive_line()! do
await broadcast(member, "{name}: {line}")
od
members.remove(member)
await broadcast(member, "{name} left")
fi
si
serve(listener: Tcp.Listener) -> Concurrency.Future is
do
let accepted = await listener.accept()
if let failed: Result.ERROR[Tcp.Connection, Error] = ►accepted then
if !(failed.error =~ Error.CLOSED) then
write_line("accept failed: {failed.error}")
fi
return
fi
Concurrency.detach(attend(accepted!))
od
si
let listener = await Tcp.listen(Endpoint(Address.LOOPBACK, 0))!
let serving = serve(listener)

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.