Hi monks!Today I have some free time and immediately took up the study of perl.
And stuck with creating simple http(tcp) server.
Problem with response.

So, this is my code:

use strict; use warnings; use IO::Socket::INET; use Date; $| = 1; my $server = IO::Socket::INET->new( LocalPort => 8080, Type => SOCK_STREAM, ReuseAddr => 1, Listen => 5, ); unless($server) { die "Can't start server on port 8080: $! \n"; } while (my $client = $server->accept() ) { $client->autoflush(1); my $request_line = <$client>; my ($method, $uri, $protocol) = $request_line =~ /(.+) \040 (.+) \ +040 (.+)/x; my $content = "Method: $method <br> Uri: $uri <br> Protocol: $prot +ocol <br>"; $content .= 'Hi there'; my $response = "HTTP/1.1 200 OK \r\n"; my $date = Date->new(time() ); $date = $date->strftime("%a, %d %b %Y %X GMT"); $response .= "Date: $date \r\n"; $response .= "Content-Type: text/html; charset=UTF-8 \r\n"; $response .= "Content-Length: " . length($content) . "\r\n"; $response .= "\r\n"; $response .= $content; print $client $response; shutdown($client, 1); close($client); } close($server);

When using curl -v http://localhost:8080, I get the response:

< HTTP/1.1 200 OK < Date: Tue, 20 Jul 2021 21:20:38 GMT < Content-Type: text/html; charset=UTF-8 < Content-Length: 61 < * Connection #0 to host localhost left intact <br>Hi there* Closing connection 0ol: HTTP/1.1

As you can see, there is truncated body.

Can you please tell, what I'am doing wrong?


In reply to TCP server with IO::Socket by AlexP

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.