Dear Monks,

I have a simple webserver that simply prints to the client, line by line, the file requested by the client.

On the other side, I have the client. The problem lies in the response that the client receives. If the requested file on the server side did not end with a \n character, the client will receive a portion of the file, but not the last line! (It follows that if the file did end with a \n, somewhere along the way, that newline will be eaten up; the client will receive the entire file but minus that last \n)

My question is, is there some way to indicate that the content is ended, without having the webserver to print out a newline regardless? I am building an automated test environment that must eventually match the response content with the original content. Among others, this is one of the reasons I'd not prefer to have extra \n's lying around. I've enclosed the corresponding client and server code are in readmore tags.

(Took out the testing of a valid request and other irrelevant stuff here).

The client:

my $remote = IO::Socket::INET->new( Proto => 'tcp', PeerAddr => $ADDRESS, PeerPort => $PORT ) or die "cannot connect to server"; $remote->autoflush(1); open(OUT, ">out.txt"); print $remote "GET /theFile.txt HTTP/1.1\r\n"; print $remote "Host: $ADDRESS:$PORT\r\n"; print $remote "Connection: close\r\n"; print $remote "\r\n"; while( <$remote> ) { print OUT $_; } close $remote;

The webserver:

$| = 1; $server = IO::Socket::INET->new( Proto => 'tcp', LocalAddr => $ADDRESS, LocalPort => $SPORT, Listen => SOMAXCONN, Reuse => 1); die "can't setup server" unless $server; while ($client = $server->accept()) { $client->autoflush(1); my $request = <$client>; if ($request =~ m|^GET /(.+) HTTP/1.[0,1]|) { $file = $1; $size = -s $file; $_ = $file; ($ext) = /.+\.(\w+)/; $type = MimeTypes::GetType($ext); # some function to get mime +type print $client "HTTP/1.0 200 OK\r\n"; print $client "Connection: Keep-Alive\r\n"; print $client "Content-Type: $type\r\n"; print $client "Content-Length: $size\r\n"; print $client "\r\n"; open(FILE, $file); while(<FILE>) { print $client $_ ; } close(FILE); } close $client; }

Any guidance would be muchos appreciated!!


In reply to webserver -- incomplete content prob by flor~

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.