I have written a Server-Client application in Java. Client is supposed to send content of directory, and Server has to save it. Unfortunately, it doesn't work for files larger than ~8 kb. After trying to send 20kb file, Client says that it successfully sent file, but it looks like Server is still waiting for a data to recieve. Why doesn't it work? Is it possible that Client closes stream before Server read all the content from it? If yes, how can I fix it?
Client:
@Override
public void run() {
try {
Socket s = new Socket(ip, port);
BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
DataOutputStream dos = new DataOutputStream(bos);
DataInputStream dis = new DataInputStream(s.getInputStream());
dos.writeInt(fileNames.size());
int k = 0;
for(File f : filesToSend) {
dos.writeLong(f.length());
System.out.println(f.length());
dos.writeUTF(fileNames.get(k));
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis);
int oneByte;
long bytesSend = 0;
long startTime = System.currentTimeMillis();
long timeElapsed;
for(int i = 0 ; i < f.length() ; i++) {
bytesSend++;
bos.write(bis.read());
timeElapsed = System.currentTimeMillis();
speed = ((startTime - timeElapsed) / 60) / bytesSend;
refresh.refreshLabels(speed, fileNames.get(k));
}
System.out.println("File "+fileNames.get(k)+" successfully sent");
k++;
fis.close();
bis.close();
}
} catch (IOException ex) {
Logger.getLogger(FileSenderSocket.class.getName()).log(Level.SEVERE, null, ex);
}
Server:
public void downloadFiles() throws IOException {
BufferedInputStream bis = new BufferedInputStream(activeSocket.getInputStream());
DataInputStream dis = new DataInputStream(bis);
DataOutputStream dos = new DataOutputStream(activeSocket.getOutputStream());
int filesCount = dis.readInt();
System.out.println("Number of files: "+filesCount);
File[] files = new File[filesCount];
for(int j = 0 ; j < filesCount ; j++) {
long fileLength = dis.readLong();
String fileName = dis.readUTF();
System.out.println("Recieving file \""+fileName+"\"");
String[] temp = (fileName).split("/");
if(temp.length > 1) {
String dir = "";
for(int k = 0 ; k < temp.length-1 ; k++) dir+=temp[k];
File dirF = new File(path.getText()+dir);
dirF.mkdirs();
}
files[j] = new File(path.getText()+fileName);
System.out.println("File Created");
FileOutputStream fos = new FileOutputStream(files[j]);
BufferedOutputStream bos = new BufferedOutputStream(fos);
System.out.println("File length: "+fileLength);
for(int i=0 ; i < fileLength ; i++) bos.write(bis.read());
bos.close();
System.out.println("File \""+fileName+"\" recieved and saved");
}
dis.close();
}
Thanks in advance
EDIT: It turns out that Server is able to recieve only 24469 bytes. When it attepts to get 24470 byte, it just pauses.