Course of Raku / Functional, concurrent, reactive, and web programming / Web programming / A simple HTTP server / Exercises / A hello server

Solution: A hello server

Here is a possible solution to the task.

Code

my $listener = IO::Socket::INET.new(
    :listen,
    :localhost('127.0.0.1'),
    :localport(8080),
);

loop {
    my $conn = $listener.accept;
    say 'a client connected';
    $conn.close;
}

🦋 You can find the source code in the file hello-server.raku.

Output

a client connected

Comments

  1. :listen with :localhost and :localport starts the server waiting.

  2. Inside the loop, .accept blocks until a client connects, prints the message, closes that one connection, and goes back to waiting for the next. This is how a server stays up to serve many clients instead of exiting after the first.

Course navigation

A hello server   |   An HTML response