Java sending multiple files one at a time

975 Views Asked by At

I have a client and server program that is supposed to work like this:

  • Client types in a filename into the command line (stdin) and the server sends the client the file data through the socket.
  • Client types in another name and another file are sent.

However, I am currently only able to get one file to be sent. The second time I type in a filename, it does not go into the while loop in my Client here:

while ((count = in.read(buffer)) > 0) {

Client (variable "fromUser" is the filename of the requested file from the server, outCommands is the outgoing datastream that transmits this filename to the Server):

while (true) {
    fromUser = stdIn.readLine();
    if (fromUser != null) {
        outCommands.println(fromUser);

        while ((count = in.read(buffer)) > 0) {
            String fileName = "downloaded" + in.readUTF();
            OutputStream fileOut = new FileOutputStream(fileName);

            try {
                fileOut.write(buffer, 0, count);
                fileOut.flush();
            } catch (IOException e) {
            }
            fileOut.close();
            break;
        }
    } else if (fromUser.equals("Stop")) {
        in.close();
        stdIn.close();
        dlSocket.close();
    }
}

Server ("dlp" is the server socket, "out" is the outgoing datastream):

while (!(fileD = in.readLine()).equals(null)) {
    System.out.println("Line read:" + fileD);

    // Load file
    File file = new File(fileD);
    System.out.println(file.getAbsolutePath());

    System.out.println("File created...");
    InputStream fileIn = new FileInputStream(file);

    outputLine = dlp.processInput(null);

    byte[] buffer = new byte[8192];
    int count;
    while ((count = fileIn.read(buffer)) > 0) {
        System.out.println("Beginning file transfer");
        out.write(buffer, 0, count);
    }

    System.out.println("Completed file transfer");
    // Write filename to out socket
    out.writeUTF(file.getName());
    out.writeLong(file.length());
    out.flush();
}

in.close();
out.close();
clientSocket.close();

Can someone help me figure out why this is happening? I don't understand why count = in.read(buffer) is not greater than zero once the server sends over the second file after it's requested by the client.

Src: https://github.com/richardrl/downloader/tree/master/src/main/java

1

There are 1 best solutions below

3
Gal Dreiman On

Because you used break in the while loop it breaks and does not go back to the loop condition. Instead of break you should use continue - or even better, remove it as it is unnecessary at the end of a loop's scope (about to re-iterate anyway):

while ((count = in.read(buffer)) > 0) {
    String fileName = "downloaded" + in.readUTF();
    OutputStream fileOut = new FileOutputStream(fileName);
    try {
        fileOut.write(buffer, 0, count);
        fileOut.flush();
    } catch (IOException e) {

    }
    fileOut.close();
    // continue; // not needed
}

When executing the line with continue the loop stops and the next line to execute is the loop condition. As said, this isn't required here since you're about to re-iterate anyway.