Reputation: 1058
I am writing Client/Server using java sockets. There is my code:
SERVER:
public void sendFile(File file) {
BufferedOutputStreambufferedOutputStream = new BufferedOutputStream(socket.getOutputStream());
int count;
FileInputStream in;
try {
in = new FileInputStream(file);
byte[] mybytearray = new byte[(int) file.length()];
while ((count = in.read(mybytearray)) > 0) {
bufferedOutputStream.write(mybytearray, 0, count);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
CLIENT:
public void downloadFile() {
BufferedInputStream bufferedInputStream = new BufferedInputStream(socket.getInputStream());
byte[] aByte = new byte[8192];
int count;
FileOutputStream in;
try {
in = new FileOutputStream("C://fis.txt");
while ((count = bufferedInputStream.read(aByte)) > 0) {
System.out.println(count); // <- nothing happens
in.write(aByte, 0, count);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Why bufferedInputStream in 2nd function is empty ?
Upvotes: 0
Views: 189
Reputation: 159844
BufferedOutputStream
will not write data until the buffer is full. You need to flush the OutputStream
:
bufferedOutputStream.flush();
Upvotes: 3