Yet another write/readObject problem

Hi! First time poster but avid reader! .. so be nice to me ^^

I'm writing a client/server program that is supposed to synchronize files from a folder chosen on the server and vice versa for the client. The problem is that my file transfer works just fine for one file, but as soon as it becomes more than one it screws up. Basically it puts all the input from the inputStream into the first file sent. I have tried a couple of solutions, but can't seem to get the hang of it. So i'll post my code as is right now. Using ObjectOutput/ObjectInputStream btw.

Server, the reciver method.

publicvoid reciveFile(String[] wantedFiles){

byte[] buffer =newbyte[BUFFER_SIZE];

//BUFFER_SIZE == 4096

int len=0,lenght,remaining;

File recivedFile;

try{

FileOutputStream fos=null;

for(int i=0; i< wantedFiles.length;i++){

recivedFile =new File(path+"/"+wantedFiles[i]);

fos =new FileOutputStream(recivedFile);

remaining = lenght = input.readInt();

System.out.print(lenght+" : "+wantedFiles[i]+":");

while ((len = input.read(buffer))>0 || remaining <=0){

fos.write(buffer, 0, len);

fos.flush();

System.out.print("#");

remaining -= len;

}

System.out.println("\nRecieved file: "+ wantedFiles[i]);

output.flush();

}

fos.close();

}catch (FileNotFoundException fnfe){fnfe.printStackTrace();}

catch (IOException ex){ex.printStackTrace();}

}

The client sender method

privatevoid sendFile(String[] serverRequiredFiles)throws InterruptedException{

FileInputStream requiredFile=null;

byte[] buffer =newbyte[BUFFER_SIZE];

int len=0;

int totalFileSize=0;

int remaining = 0;

try{

for(int i=0;i < serverRequiredFiles.length;i++){

requiredFile =new FileInputStream(path+"/"+serverRequiredFiles[i]);

remaining = totalFileSize = requiredFile.available();

System.out.println(totalFileSize);

output.flush();

System.out.print(serverRequiredFiles[i]+": ");

while ((len = requiredFile.read(buffer)) !=-1){

output.write(buffer,0,len);

output.flush();

remaining -= len;

if (remaining <= 0){

output.write(-1);

output.flush();

}

System.out.println("\nDone!");

}

System.out.println("\nDone! All files sent ");

requiredFile.close();

}

}catch (FileNotFoundException ex){

ex.printStackTrace();

}catch (IOException ex){

ex.printStackTrace();

}

}

As i said earlier, it looks like everything from the inputStream gets thrown into the same file. Well.. look is the wrong word .. it does. The client side on the other hand send all files successfully. As it should..

Can you pleas help me .. because I really can't fix this on my own. =/ I have entered the desperate zone.

[4986 byte] By [Caine_72a] at [2007-11-27 0:50:52]
# 1

Try to open the File and the FileOutputStream inside the for loop.

Something like:

try

{

for( int i=0; i< wantedFiles.length;i++)

{

File recivedFile = new File(path+"/"+wantedFiles[i]);

FileOutputStream fos = new FileOutputStream(recivedFile);

remaining = lenght = input.readInt();

System.out.print(lenght+" : "+wantedFiles[i]+":");

while ((len = input.read(buffer))>0 || remaining <=0)

{

fos.write(buffer, 0, len);

fos.flush();

System.out.print("#");

remaining -= len;

}

fos.close();

System.out.println("\nRecieved file: "+ wantedFiles[i]);

output.flush();

}

}

catch (FileNotFoundException fnfe)

{fnfe.printStackTrace();}

catch (IOException ex)

{ex.printStackTrace();}

Best Regards

Danniel_Williana at 2007-7-11 23:21:23 > top of Java-index,Java Essentials,Java Programming...
# 2

Hi,

The problem is that Java is doing exactly what your ask for :

As there something in the stream : write it to the first output stream (file)

Then manage the second file.

The problem is coming from your protocol. I would suggest, when sending a file

to send first the file size, then on the other side you first read that size, then read

size bytes, write then in the output stream, then process the next file.

Hope that help,

Jack

jack@square6a at 2007-7-11 23:21:23 > top of Java-index,Java Essentials,Java Programming...
# 3

> The problem is that Java is doing exactly what your

> ask for :

> As there something in the stream : write it to the

> first output stream (file)

> Then manage the second file.

Hmm .. ok ..I thought me sending the -1 took care of that .. as it's suppose to break the loop if the len = input.read(buffer))>0 .. and I send a -1 if remaining <=0 on the client side.

> The problem is coming from your protocol. I would

> suggest, when sending a file

> to send first the file size, then on the other side

> you first read that size, then read

> size bytes, write then in the output stream,

> then process the next file.

well actually. I accidentally erased the code snippet that sent the file size to the server. So that is actually sent. On the other hand, what the server recives is the summed up total amount for all the files and not only for the actual first file. How that happens i really don't know thought :P ..

> The problem is coming from your protocol. I would

> suggest, when sending a file

> to send first the file size, then on the other side

> you first read that size, then read

> size bytes, write then in the output stream,

> then process the next file.

Okeeej .. must be some syntax error in my head as thats how i read it ( in a matter of speaking, not exactly that way thought). But then again my program dosn't work in current state so I cant claim to be right either ^^

Please could you show me a code example of your id閍 ? Or someone else as a matter of fact :) ..Open to suggestions as i'm going bonkers over here < : P

best regards,

Caine

*Edit

Started working on the ideas given here and found out that my client-side script was a tad screwed up so i will repost that one.

Message was edited by:

Caine_72

Caine_72a at 2007-7-11 23:21:23 > top of Java-index,Java Essentials,Java Programming...
# 4

the correct client code

private void sendFile(String[] serverRequiredFiles) throws InterruptedException{

FileInputStream requiredFile=null;

byte[] buffer = new byte[BUFFER_SIZE];

int len=0;

int totalFileSize=0;

int remaining = 0;

try {

for(int i=0;i < serverRequiredFiles.length;i++){

requiredFile = new FileInputStream(path+"/"+serverRequiredFiles[i]);

remaining = totalFileSize = requiredFile.available();

System.out.println(totalFileSize);

output.write(totalFileSize);

output.flush();

System.out.print(serverRequiredFiles[i]+": ");

while ((len = requiredFile.read(buffer)) !=-1){

output.write(buffer,0,len);

output.flush();

remaining -= len;

if (remaining <= 0){

System.out.println("send -1");

output.write(-1);

output.flush();

}

}

System.out.println("\nDone!");

}

System.out.println("\nDone! All files sent ");

requiredFile.close();

} catch (FileNotFoundException ex) {

ex.printStackTrace();

} catch (IOException ex) {

ex.printStackTrace();

}

}

Caine_72a at 2007-7-11 23:21:23 > top of Java-index,Java Essentials,Java Programming...
# 5

Ok, I finally solved it by myself. And as I find it irritable when people solve there problem but not their solution I decided to do a follow-up and post mine. ^^

Hope this helps someone out there in cyberspace. Thank you for the responses btw.

ServerSide

public void reciveFile(String[] wantedFiles) {

byte[] buffer = new byte[BUFFER_SIZE];

int len=0,totalFileSize=0,remaining=0;

File recivedFile=null;

try {

for( int i=0; i< wantedFiles.length;i++){

recivedFile = new File(path+File.separator+wantedFiles[i]);

FileOutputStream fos = new FileOutputStream(recivedFile);

totalFileSize = input.readInt();

while (remaining != totalFileSize){

if((len = input.read(buffer))!=-1){

fos.write(buffer, 0, len);

fos.flush();

System.out.print("#");

}

remaining = remaining + len;

}

len = 0;

remaining= 0;

System.out.println("\nRecieved file: "+ wantedFiles[i]);

fos.close();

}

} catch (FileNotFoundException fnfe) {fnfe.printStackTrace();}

catch (IOException ex) {ex.printStackTrace();}

}

clientSide

private void sendFile(String[] serverRequiredFiles) throws InterruptedException{

FileInputStream requiredFile=null;

byte[] buffer = new byte[BUFFER_SIZE];

int len=0;

int totalFileSize=0;

int remaining = 0;

double percentage = 0, oldPercentage =0;

try {

for(int i=0;i < serverRequiredFiles.length;i++){

requiredFile = new FileInputStream(path + File.separator + serverRequiredFiles[i]);

totalFileSize = requiredFile.available();

System.out.println(totalFileSize);

output.writeInt(totalFileSize);

output.flush();

System.out.print(serverRequiredFiles[i]+": ");

while ((len = requiredFile.read(buffer)) !=-1){

output.write(buffer,0,len);

output.flush();;

}

len = 0;

System.out.println("\nDone!");

}

System.out.println("\nDone! All files sent ");

requiredFile.close();

} catch (FileNotFoundException ex) {ex.printStackTrace();

} catch (IOException ex) {ex.printStackTrace();

}

}

Caine_72a at 2007-7-11 23:21:23 > top of Java-index,Java Essentials,Java Programming...
# 6
Exactly. The problem was here:> while ((len = input.read(buffer))>0 || remaining <=0)which never made any sense. Wrong operator, wrong 2nd test, and wrong order of tests.
ejpa at 2007-7-11 23:21:23 > top of Java-index,Java Essentials,Java Programming...
# 7

> Exactly. The problem was here:

> > > while ((len = input.read(buffer))>0 || remaining

> <=0)

>

which never made any sense. Wrong operator,

> wrong 2nd test, and wrong order of tests.

Well actually that test was just written for a test run, and happened to stay there for a run or two. Mind you that i didn't work before or after i edited it away. It started working as soon as i completely moved the ((len = input.read(buffer))!=-1)

out of the while loop and into the if-case inside of the loop.

Hence:

while (remaining != totalFileSize){

if((len = input.read(buffer))!=-1){

fos.write(buffer, 0, len);

fos.flush();

System.out.print("#");

}

remaining = remaining + len;

}

But as I have understood it the while ((len = input.read(buffer))>0)

is fully valid if you do just one file transfer... just don't add the remaining variable there .. ^^ But as I said desperation .. and 15 hours of screaming can do that to you.. So many ~strange~ and different combinations there where ^^

Caine_72a at 2007-7-11 23:21:23 > top of Java-index,Java Essentials,Java Programming...