Building Our Server

Now that we know how all the system calls work, let’s put them together into working code. Rather than try to deal with concurrency and sockets in one go, we’ll first build a nonconcurrent server that we can test for correct implementation of the socket-handling patterns. Then, once we’ve verified its correctness, we’ll modify it to handle multiple concurrent connections.

In the next chunk of code, we’ll create the sockaddr_in struct that describes the port and address to bind to. Then, we’ll invoke socket() and bind, and check their results, before looping over accept() and handling incoming connections.

 def​ serve(port​:​​UShort​)​:​ ​Unit​ = {
 // Allocate and initialize the server ...

Get Modern Systems Programming with Scala Native now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.