Chapter 36: a server and its own client in one process, built
A network program does not have to be two programs. This lesson builds chapter 36's first example, a TCP server and its own client in ONE process: the server asks the operating system for a free port, so nothing is fixed and nothing outside the machine can reach it. echo is a named function that implements TCPHandler - the dialog's Extends field - and the program runs both ends. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-echo-in-process.
What you'll build
The lesson builds this program from a file holding only its module line - the finished file, as the book prints it.
#!ek9 defines module book.network.echo defines function echo() is TCPHandler -> input as StringInput output as StringOutput while input? output.println(`echo:${input.next()}`) defines program EchoInProcess() stdout <- Stdout() props <- NetworkProperties(port: 0, backlog: 2, maxConcurrent: 2, localOnly: true) try -> tcpServer <- TCP(props) client <- (stdout, props) is TCPReadyHandler as function loopback <- Networking().loopbackV4() try -> connection <- TCP(NetworkProperties(loopback, props.port)).connect() if connection? connection.output().println("hello") stdout.println(`client received [${connection.input().next()}]`) server.stop() if tcpServer.acceptUntilComplete(echo, client) stdout.println(`served until the client finished, server still set: ${tcpServer?}`) stdout.println("the try has closed the server") //EOF
Step 1: echo — a function
Choose Code > New Construct > Function…
echo is the function's name. Add the parameters input of type StringInput and output of type StringOutput, type TCPHandler into Extends. Choose Preview.
The preview shows echo about to be written. Choose Apply.
Select the echo() is TCPHandler line and, above it, type the blank line the book opens its first function with. Saves when typed.
Select the stub, from require input? down to its //AI line, and type echo's own return and work, as the book writes them. Saves when typed.
Step 2: EchoInProcess — a program
Choose Code > New Construct > Program…
EchoInProcess is the program's name. Choose Preview.
The preview shows EchoInProcess about to be written. Choose Apply.
Select the EchoInProcess line and, above it, type the blank line the book opens the block with. Saves when typed.
Select the stub, from ready <- true down to its //AI line, and type the program exactly as the chapter shows it. Saves when typed.
Step 3: Run it
Choose Run > Run Program…
The project's only program is already chosen. Choose Run.
The client received echo:hello, the server was still set when it stopped, and the try had closed it by the last line. Choose Close.
The result
EchoInProcess.ek9 now reads exactly as chapter 36 of EK9 in Depth shows it: a server that is stopped by its own client and closed by the try that owns it - nothing in the program says close.
Where this lesson comes from
- For AI assistants - the MCP tool
ek9_new_construct,ek9_add_program,ek9_run_program.













