0

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.

Romen
  • 103
  • 8
  • You shouldn't be reading directly from `bis` after it has been wrapped in a `DataInputStream`. Read only directly from `dis`. – RealSkeptic Nov 18 '15 at 21:50
  • 1
    Also, you'll want to double check filenames.size() versus the number of File objects in the collection "filesToSend" . I see that you're telling the server that you're going to be sending "filenames.size()" files, and send using the File objects in "filesToSend". From the code, I can't be sure of the relationship between "filenames" and "filesToSend". – Code4aliving Nov 18 '15 at 21:59
  • @BradBales Checked and it's fine :) Also, smaller files are being sent just fine. – Romen Nov 19 '15 at 08:47
  • @RealSkeptic I've used DataOutputStream and DataInputStream instead of their Buffered counterparts, but it still hangs on. – Romen Nov 19 '15 at 08:50
  • And the same goes for the `BufferedOutputStream` in the client: you shouldn't use it once you have wrapped it. Use only the `DataOutputStream`. – RealSkeptic Nov 19 '15 at 08:52
  • Yep, changes that as well but it still fails :( – Romen Nov 19 '15 at 09:51
  • @RealSkeptic Thanks a lot! Your comment made me to give some thoughts to streams. I wrapped socket streams directly into DataInputStream and DataOutputStreams, instead of using Buffered streams "in the middle" and it now works fine. – Romen Nov 19 '15 at 16:52

0 Answers0