in reply to Re^7: sysread and syswrite in tcp sockets
in thread sysread and syswrite in tcp sockets
The typical solution is for the sender to prefix the data with the number of bytes. Then the reader can read (say) a 4 byte integer indicating how many bytes will follow, and then issue a read for the approriate number of bytes, or better, loop reading reasonable size chunks and counting until it has it all:
## sender my $size = -s 'theFileToSend'; print $socket pack 'N', $size; ## Assumes files less than 4GB. local $/ = \2**16; ## read the file in 64k chunks print $socket $_ while <$file>; ## The reader my $size; read( $sock, $size, 4 ); ## Get the size in binary $size = unpack 'N', $size; while( $size > 0 ) { my $buffer; my $chunkSize = $size < 2**16 ? $size : 2**16; $size -= read( $sock, $buffer, $chunkSize ); ## do something with this chunk }
|
|---|