0

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?

asked Mar 6, 2023 at 16:01
6
  • add the code that causes the issue, add the exception being thrown and put specific problem you're facing not examples Commented Mar 6, 2023 at 16:09
  • Probably you should change the approach of the implementation, One approach you can take is to establish two separate input/output streams between the client and the server: one for user input and another for file data. When the client sends a "put" command, the server can send a message back to the client indicating that it is ready to receive the file data. The client can then send the file data over the second input/output stream, while the first input/output stream remains available for the client to input additional commands. Commented Mar 6, 2023 at 16:42
  • have a look at design patterns that can solve your issue in particular, for further reading: redhat.com/architect/…. Commented Mar 6, 2023 at 16:44
  • Please provide enough code so others can better understand or reproduce the problem. Commented Mar 7, 2023 at 4:44
  • Why don't you use ready-made FTP library? Commented Mar 7, 2023 at 7:16

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.