• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Transfer multiple files from Server to Client using Sockets

 
Nathan Heimdall
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

A project that I'm working on needs to pass 3 text files from the Server to the Client.
This what I'm doing
Server side :Wait for client, once connected to client pick up the files and transmit it to the client one after another.
Client side :Connect to server and accept the files one after an other.

I'm only able to receive the first txt file after which I'm getting an ArrayIndexOutOfBoundsException.
In debug mode I noticed that no data is being read on the second read segment on the client side.

Here is a segment of the server side code:



This is the client side code :



Any help on how to synchronize the file sending so the client reads all three files is much appreciated
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see in your code how the receiver is supposed to tell where one file ends and the next one starts. It has this mysterious "filesize" variable which I don't see where that comes from or what it's set to. Do you only ever plan to send files of that size and no other size?
 
Nathan Heimdall
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr.Clapman,

That is the problem I'm having, I don't know how to inform the receiver that the first file has ended and the second has started.
How can put such a system of notification into place?
Also I don't plan to fix the size of the files as all three files are of different sizes
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your protocol needs to be redesigned so it knows when a new portion is read. An example of what could be sent:
- an int indicating the name's length when converted to byte[] using a fixed charset (e.g. UTF-8)
- the file name as a byte[] using that charset
- an int indicating the file size
- the file in bytes.

In short, the sending code:
The receiving code:
 
Nathan Heimdall
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mr.Prime,
I changed it and it worked perfectly
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic