client/server file send problem
(These are just the relevant snippets from my program for my question. out and in are the socket streams, im sure you can see what the rest is)
This snippet here is for the client to send the file to the server, encrypted..
Code:
Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
// Send file
FileInputStream fin = new FileInputStream(file);
byte[] buffer = new byte[8192];
byte[] bout = null;
int length;
while (true) {
length = fin.read(buffer);
out.writeInt(length);
if (length == -1) break;
bout = cipher.update(buffer, 0, length);
out.write(bout);
}
and the server decrypting the file and putting it together on the server..
Code:
Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
// receive file
FileOutputStream fout = new FileOutputStream(file);
byte[] buffer = new byte[8192];
byte[] bout = null;
int length;
while (true) {
length = in.readInt();
if (length <= 0) break;
in.read(buffer, 0, length);
bout = cipher.update(buffer, 0, length);
fout.write(bout);
}
Everything works fine, except 1 problem. On the client side, say i want to send test.txt and test.txt contains this:
Code:
Hello there
whats up
Multi line test stuff!!
on the server side, the file ends up like this
Code:
Hello there
whats up
Multi line
on the last line, it should say: Multi line test stuff!!
anyone see what im doing wrong?
Re: client/server file send problem
read() and write() with sockets are not guaranteed to return as much data as requested. Perhaps that's the problem?