Forums Register Login

Copying Files over a Socket

+Pie Number of slices to send: Send
This may be more of an I/O question...

I want to copy files over a network reliably. The program I have works except for two things:
  • The socket connection should terminate after receiving the file. In runtime, the connection does not terminate. My guess is, the client is stuck in the for loop that reads the file from the network.
  • When I used a test file, numbering the lines from 1 to 1000 (each lines reads "[number] ----- L" ), I noticed about 60 extra lines at the end of the file. Probably this is because I read into and copy from 1024-byte arrays which don't totally get written over at the end of the file.


  • So, how do I detect the end of file when I am using byte arrays like this? Or is there an even bigger issue.



    +Pie Number of slices to send: Send
     

    J Mangual wrote:
    When I used a test file, numbering the lines from 1 to 1000 (each lines reads "[number] ----- L" ), I noticed about 60 extra lines at the end of the file. Probably this is because I read into and copy from 1024-byte arrays which don't totally get written over at the end of the file.



    Have a look at our FAQ: ReadDoesntDoWhatYouThinkItDoes, though in this case, write() doesn't do what you think it does. You always write 1024 bytes no matter how many bytes you have read.
    Also, I believe if you do not close the output stream of the socket, EOF is never sent, so your client will just block waiting for input. Why did you comment out the socket.close()? Try flushing and closing the output stream.
    +Pie Number of slices to send: Send
    Is the FAQ suggesting we are supposed to load the entire file into memory when we receive it?

    Here's a quote from Eliotte Rusty Harold's "Java I/O":

    Traditional synchronous I/O is designed for traditional applications...

  • Files may be large but not huge. It's possible to read an entire file into memory.
  • An application reads or write only to a few files or network connections at the same time, ideally using only one stream at a time
  • The application is sequential. It won't be able to do muc until it's finished reading or writing a file.


  • So in the FAQ it looks like they read the whole file into the buffer and then process it. Do I need to read the whole file into memory just to copy it accurately?

    A correct usage of read() would be to read chunks of a file and collect them in a buffer like so:




    +Pie Number of slices to send: Send
     

    J Mangual wrote:Is the FAQ suggesting we are supposed to load the entire file into memory when we receive it?



    The FAQ is suggesting that you write only the data you receive. Note the use of the variable "read":


    As the FAQ says, the call to read() may not fill the buffer temp. Your code always writes 1024 bytes. The code in the FAQ will only write as many bytes as is read.
    It just so happens that in the example used in the FAQ, the file is to be read into memory fully, then processed.
    +Pie Number of slices to send: Send
    I changed it so that it reads a variable number of bytes and there is no extra goop at the end of the file now. I try to read and write to the buffer as much as possible, but I realize there is no guarantee to how many bytes are actually written.

    The reason I don't want to close the output stream is because I may want to send several files over the same connection. So it's important I find some way of getting the end of file, but maybe that's for another thread. Thanks.

    Client:

    Server:
    +Pie Number of slices to send: Send
     

    J Mangual wrote: I try to read and write to the buffer as much as possible, but I realize there is no guarantee to how many bytes are actually written.



    Try searching the forum. I'm certain we've discussed that topic more than once.
    The two armies met. But instead of battle, they decided to eat some pie and contemplate this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 5726 times.
    Similar Threads
    Copying Multiple Files Over A Socket
    Socket connection program works for localhost but not for other ip
    Sending a file over socket
    How to Change the File Name for Each Uploaded Files to the Socket Server?
    Copying Files over a Socket
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Apr 16, 2024 08:43:48.