Problems transferring multiple files

I'm trying to write a program to transfer multiple files from a client to a server, but I'm having trouble getting the server to distinguish between them.

I have tried both buffered streams, and straight input/output streams, and in both cases, the server doesn't seem to get any EOF, and keeps reading all data it reads into the first file. I can't figure out why EOF is never read (ie: ((read = bis.read(buffer)) != -1) never evaluates to -1). Can anybody see why that might be?

Client:

buffer =newbyte[16384];

out= serverSocket.getOutputStream();

bos=new BufferedOutputStream(out);

objOut =new ObjectOutputStream(out);

objOut.writeInt(file.length);// # of files to transfer

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

try{

fileIn =new FileInputStream(file[i].getAbsolutePath());

objOut.writeLong(file[i].length());// file length (for dirty CRC check)

objOut.writeObject(file[i].getName());// file name

objOut.flush();// because otherwise the server hangs waiting

while ((read = fileIn.read(buffer)) > 0){

bos.write(buffer, 0, read);

bos.flush();

}

}

catch (Exception e){}

}

Server:

in = socket.getInputStream();

objIn =new ObjectInputStream(in);

bis =new BufferedInputStream(in);

fileCnt = objIn.readInt();// # of files to be transferred

for (int i=0; i<fileCnt; i++){

totalLen = objIn.readLong();// file length

try{ fileName = (String)objIn.readObject();}// file name

catch (Exception e){}

try{ toFile =new FileOutputStream(new File(fileName));}

catch (FileNotFoundException ex){}

while ((read = bis.read(buffer)) != -1){

toFile.write(buffer, 0, read);

}

}

>

[3249 byte] By [m4trixa] at [2007-10-2 22:14:30]
# 1

the problem is you are not stoping the writing to file when you hit the end of one file.

At the server you should count the number of bytes that you write to the disk and should stop when the total count reach the file size.

Or you have to completly change your protocole to handle entire file as one unit.

Ex:-

public class MyFile implements java.io.Serializable{

String path;

private void writeObject(ObjectOutputStream oos){

oos.defaultWriteObject();

byte b[] = new byte[1024*128];

int i = 0;

FileInputStream fis = new FileInputStream(path);

while ((i = fis.read(b)) >=0){

oos.writeObject(trim(b,i));

}

fis.close();

oos.writeObject("EOF");

}

private byte[] trim(byte b[], int i){

byte b2[] = new byte[i];

System.arraycopy(b,0,b2,0,i);

return b2;

}

// You should be able to write the read object method

}

LRMKa at 2007-7-14 1:31:27 > top of Java-index,Archived Forums,Socket Programming...
# 2

The problem is that you are expecting an EOF to appear in the middle of the stream and it won't. It will only appear when the other end closes the socket.

The suggestion above means that you should prepend a length word to each file so you know when you've read it all, or use ObjectOutputStream.writeObject() somehow.

ejpa at 2007-7-14 1:31:27 > top of Java-index,Archived Forums,Socket Programming...
# 3
Ahhh... I just misunderstood how it worked.Thanks a lot for the help guys. I'll rework it & post back here if I have any problems :)ty
m4trixa at 2007-7-14 1:31:27 > top of Java-index,Archived Forums,Socket Programming...