aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Transfer multiple files from Server to Client using Sockets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Transfer multiple files from Server to Client using Sockets" Watch "Transfer multiple files from Server to Client using Sockets" New topic
Author

Transfer multiple files from Server to Client using Sockets

Nathan Heimdall
Greenhorn

Joined: Mar 02, 2009
Posts: 20
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


-Nathan
"A single conversation with a wise man is better than ten years of study."
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18166
    
    8

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

Joined: Mar 02, 2009
Posts: 20
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

Joined: Oct 27, 2005
Posts: 19557
    
  16

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Nathan Heimdall
Greenhorn

Joined: Mar 02, 2009
Posts: 20
Thanks Mr.Prime,
I changed it and it worked perfectly
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19557
    
  16

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Transfer multiple files from Server to Client using Sockets
 
Similar Threads
client-server file exchange
Sending a file over socket
Socket connection program works for localhost but not for other ip
copying files over a network
File outputted has size zero