Course of Raku / Functional, concurrent, reactive, and web programming / Web programming / Making remote connections / Exercises / Send and receive
Solution: Send and receive
Here is a possible solution to the task.
Code
my $conn = IO::Socket::INET.new(:host('example.com'), :port(80));
$conn.print("HEAD / HTTP/1.0\r\nHost: example.com\r\n\r\n");
my $response = $conn.recv;
$conn.close;
say $response.lines.first;🦋 You can find the source code in the file send-and-receive.raku.
Output
HTTP/1.1 200 OKComments
.printsends the request; the protocol lines end in\r\n. AHEADrequest asks the server for just the status line and headers, so the reply is small..recvreads the reply, and.lines.firsttakes its first line — the HTTP status line.