Slide 31 of 37

Networking: raw TCP

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)
}
Raw TCP is a stream, not a queue of messages, so hive.net's socket calls move bytes rather than whole messages. socketConnect dials out and socketServe(port, handler) listens, calling a proc(SocketConnection): void once per connection; socketReceive(conn, bytes) does a short read — fewer bytes than asked for is normal, not an error — while socketReceiveLine reads up to the next newline and drops it. socketPeer names who is on the other end, and socketClose ends the stream. A SocketError's reason is Connect, Closed, Send or Receive.