I'm writing a class for 2-way sending file via Sockets in Java Here on GitHub is it. Everything is good until file receiving finished. Shortly:
On client:
byte[] bytes = new byte[(int)forSend.length()];
InputStream fin = new FileInputStream(forSend);
int count;
while ((count = fin.read(bytes)) > 0) {
out.write(bytes, 0, count);
}
fin.close();
fout = new FileOutputStream(filename);
byte[] bytes = new byte[length];
System.out.println("receiving file...");
int count;
while ((count = in.read(bytes)) > 0) {
fout.write(bytes, 0, count);
}
fout.flush();
fout.close();
When I'm trying to add code for writing something to socket after that, after start server and client are waiting for something (I don't know what)
Previously I meet this situation when lost one DataInputStream reading (message sent from server but there was no reciever on client for this message). But currently I'm trying to add flag which is changed after file transfer finished and check for it's state later. It's work both on server and client, but adding read/write from/to Socket return me back to situation when both server and client are wait for something.
What's wrong now?
My friend Denr01 helped me, so my mistake was control of file length, I don't have it anywhere in my question. And because of that my "finishing" confirmation was writen to file. The way to solve problem is in sender:
int read = 0;
int block = 8192;
int count = 0;
byte[] bytes = new byte[block];
while (read != forSend.length()) {
count = fin.read(bytes, 0, block);
out.writeInt(count);
out.write(bytes, 0, count);
read += count;
System.out.println("already sent " + read + " bytes of " + forSend.length());
}
In sender:
int block = 8192;
int count = 0;
int read = 0;
byte[] bytes = new byte[block];
System.out.println("recieving file...");
while (read != length) {
block=in.readInt();
in.readFully(bytes, 0, block);
fout.write(bytes, 0, block);
read += block;
System.out.println("already recieved " + read + " bytes of " + length);
}
In this case we have control of each file reading iteration and always know how many bytes to recieve, so when all the bytes recieved files are identical and next "messages" will not be writen into file.