I'm using sockets for file transfer in java. Here is the Client code
for(int i = 0;i < fileList.size();i++) {
String filename = (String)fileList.get(i);
RequestFile(filename);
try {
BufferedOutputStream fileWriter = new BufferedOutputStream(
new FileOutputStream(
new File(PROGRAM_PATH + "/" +
filename)));
int packet;
int count = 0;
while((packet = fileReader.read()) != -1) {
fileWriter.write(packet);
count++;
}
System.out.println(filename + " receiver complete. (count : " + count + ")");
fileWriter.flush();
fileWriter.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
And here is the Server code
public void SendFile(String filename) {
try {
fileReader = new BufferedInputStream(new FileInputStream(CLIENT_PATH + "/" + filename));
int packet;
int count = 0;
while((packet = fileReader.read()) != -1) {
count++;
fileWriter.write(packet);
}
fileWriter.write(-1);
System.out.println(count);
fileReader.close();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
When I checked the server's count, it was 635
. It means the server had sent data 635
times.
However, the client's print count is only 512
. (from 0 to 511) I think it's stopped at read()
, because
System.out.println(filename + " receiver complete. (count : " + count + ")");
is not printed. Can someone tell me the reason and solution?