How to properly receive data from input/output streams in Multithreaded Server Client FTP

34 Views Asked by At

I'm trying to create a Multithreaded Server Client FTP that will allow the client to perform certain tasks in the background (EX: put test.txt) can run in the background and then the client can input another command. So far, I have a listener thread that allows a client to connect to the server via a socket. I also have a worker thread that can perform get/put (the only commands that I want to run in the background)

The issue that I am having though is that when I am trying to run put, for example, in the background, the listener thread is reading the input from the file that is being sent over from the client and is assuming that this input is a new command, which is causing an error.

This is the Listener Thread Code:

try {
                        command = input.readUTF();
                        String[] splitCommand = command.split("\\s+");
                        String fileName = "";
                        if (splitCommand.length > 1)
                            fileName = splitCommand[1];
                        executeCommand(Integer.valueOf(splitCommand[0]), fileName);
                    } catch (IOException i) {
                        System.out.println(i);
                        System.exit(0);
                    }

This is the Worker Thread Code:

File file = new File(currDirectory + fileName);
file.createNewFile();
String data = "";
data = input.readUTF();
fileWriter = new FileWriter(currDirectory + fileName);
fileWriter.write(data);
fileWriter.close();

I'm receiving a NumberFormatException because the Listener Thread is receiving the contents of the file and is assuming that the contents are a new command.

How do I differentiate from client input that is from the user and client input that is a file being sent over? Can I use multiple input/output streams?

0

There are 0 best solutions below