So I spent the last few hours trying to figure out how to get my client-server system (which now uses sockets) to transfer files. The final results is:

For the server:
sub SendFile { my($to, $file) = @_; print $to "Sending $file:\n"; open(IN, $file) || die("Could not open $file: $!\n"); binmode(IN); print $to <IN>; print $to "\nEND\n"; close IN; }
and for the client:
sub RecieveFile { my($server) = $_[0]; $line = <$server>; $line =~ /Sending (.*):/; print("Receiving $1...\n"); open(OUT, ">$1.cpy") || die("Cannot open $1.cpy: $!\n"); binmode(OUT); while(($_ = <$server>) ne "END\n") { print OUT; } close(OUT); print("Finished\n"); }
Originally I tried to do this without the little END tag; however that produced odd results. The client would seem to hang until the server was killed, at which point the client would recieve the file. Because I didn't know whether this one was should happen or not, I wrote the following test program:
use strict; open(IN, $ARGV[0]); binmode(IN); open(OUT, ">$ARGV[0].cpy"); binmode(OUT); print OUT <IN>; close(OUT); close(IN);
This program copies the file specified in its first agrument. Moreover this program works perfectly. Then I guessed that maybe (due to the client-server system) there was no way to specify that I had finished sending the binary output, so I added a newline character where the END tag currently is. That had the unfortunate result of sending will more then triple the actual file size to the program. Thus I came up with the END tag you see here.

My question is: "Why did that stuff happen?" Am I missing some fundamental concept?

Thanks,
Boots

In reply to client server file transfer by Boots111

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.