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 connectedComments
:listenwith:localhostand:localportstarts the server waiting.Inside the
loop,.acceptblocks 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.