samedi 28 mars 2015

BufferedInputStream hanging (not reaching end of file)



I have Java SSL/TLS server&client sockets. My client simply sends a file to the Server and Server receives it. Here are my codes:


My client method:



static boolean writeData(BufferedOutputStream bos, File data) {
FileInputStream fis = new FileInputStream(data);
BufferedInputStream bis = new BufferdInputStream(fis);

byte[] bytes = new byte[512];
int count = 0;
while ((count = bis.read(bytes, 0, bytes.length)) > 0) {
System.out.println("Sending file...");
bos.write(dataByte, 0, count);
System.out.println(count);
}
bos.flush();
System.out.println("File Sent");
}


My server method:



static boolean receiveData(BufferedInputStream bis, File data) {

byte[] bytes = new byte[512];
int count = 0;
while ((count = bis.read(bytes, 0, bytes.length)) > 0) {
System.out.println("Receiving file...");
// Do something..
System.out.println(count);
}
bos.flush();
System.out.println("File Received");
}


The problem is, the server hangs inside the while loop.. It never reaches the "File Received" message.


Even if the file is small, the bis.read() method never returns -1 at the end of file for some reason. I tested the methods with a file size of 16 bytes, and the output is as follows:


Client terminal:



> Sending file...
> 16
> File Sent


Server terminal:



> Receiving file...
> 16


As you can see, the server never reaches the "File Received" message and hangs inside the loop even after the end of stream is reached.. Can anyone guess the reason for this?


Thanks




Aucun commentaire:

Enregistrer un commentaire