The standard librarySlide 68 of 85
68tcpThe standard library
TCP and names
Raw TCP is a stream, not a queue of messages, so these calls move bytes rather than whole messages:
socketServe(port, handler)calls aproc(SocketConnection): voidonce per connection;socketConnect(host, port)dials out, answering with aResult.socketReceive(conn, bytes)answers with up to that many bytes — fewer is normal, not an error.socketReceiveLine(conn)reads to the next newline and drops it, a\rbefore it too, and still delivers a last line that has none.socketSend(conn, data)answers with the bytes sent;socketPeer(conn)is who is on the other end, as"host:port";socketClose(conn)ends the stream.
A hive.net.SocketError's reason is "Connect", "Closed", "Send" or "Receive".
Two calls are about names rather than connections. resolve(name) looks up every address behind a host name — a name, not "host:port" — and an address written out resolves to itself. localAddress() is the address another machine would reach this one on, never the loopback, found without sending anything. Their hive.net.NetError's reason is "NotFound", "Lookup" or "NoAddress".
proc handle(conn: hive.net.SocketConnection): void {
if hive.net.socketReceiveLine(conn) is Result.Ok(line) {
hive.net.socketSend(conn, "you said: {line}\n")
}
hive.net.socketClose(conn)
}
proc main(): void {
hive.net.socketServe(9000, handle)
}