• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Copying Multiple Files Over A Socket

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to copy several files over the same stream. These files may have different sources and different destinations, so I'm using standard input to read the source and file destination names. As written, the client program takes all the input and write it to the same destination file. I would like it to receive a file, detect end of file, close it and create a new FileOutputStream object. Closing the server socket is not an option because I would like to send many files over the same session.

Client

Server

This is really a sequel to an earlier post, but they suggest posting different questions in different threads.
 
Sheriff
Posts: 28413
102
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't detect when the other machine stops sending a file. Unless it closes its socket, which you said you don't want. So you need a protocol which tells you how to tell when you have read the whole file.

One easy protocol is to send the number of bytes in the file first, then to send that many bytes. The receiver will read the number of bytes, then it will read that many bytes.
 
J Mangual
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All kinds of care has to be made transmitting the file size because read() and write() do not behave reliably. This one seems to work.

Client:


Server:
 
Paul Clapham
Sheriff
Posts: 28413
102
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

J Mangual wrote:All kinds of care has to be made transmitting the file size because read() and write() do not behave reliably.



Well, not really. I would just use a DataOutputStream to send the file size (without looking I would guess the method is writeLong) and a DataInputStream to read it at the other end. I'm not aware of any unreliable aspects of that.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic