0

Client asks the server to delete a file from server. In the code below there are three lines of code with the comment "Problematic". The program below succeeds in deleting the file and then returns to service more requests from the client if I do not include the lines marked "Problematic". If these lines are included in the code then the requested file does not get deleted and the client and server both hang. What is wrong with this code? (Thanks in advance)

Client side:

//...
client1 = new Socket(ip, port);
inFromServer = new BufferedReader(new InputStreamReader(client1.getInputStream()));
outToServer = new DataOutputStream(client1.getOutputStream());

Scanner scan0 = new Scanner(System.in);
String command = scan0.nextLine();

switch (command) {

//Other cases...

case "Delete file":
    outToServer.writeBytes(command + '\n');
    System.out.print("Enter file name: ");
    Scanner del_file = new Scanner(System.in);
    String file_name = del_file.nextLine();
    outToServer.writeBytes(file_name);
    System.out.println("\n" + inFromServer.readLine() + "\n");//Problematic

    client1.close();
    break;

Server side:

server1accept = server1.accept();
inFromClient = new DataInputStream(server1accept.getInputStream());
outToClient = new DataOutputStream(server1accept.getOutputStream());

BufferedReader readIncoming = new BufferedReader(newInputStreamReader(server1accept.getInputStream()));         
String clientMsg = readIncoming.readLine();
String server_file_location = "D:/"; //server file storage location

switch (clientMsg) {
    //other cases

case "Delete file":
    System.out.print("Command from ip: " + server1accept.getInetAddress() + " port: "+ server1accept.getPort()+": ");
    System.out.println("Delete file");

    String del_file = readIncoming.readLine();
    String path = server_file_location + del_file;

    if(Files.exists(Paths.get(path)) == true){                      
        System.out.println("Deleting file: " +Paths.get(path));
        Files.delete(Paths.get(path));
        System.out.println("Deleted");
        outToClient.writeBytes("Deleted");//Problematic
    }
    else{
        System.out.println("Requested file does not exist");
        outToClient.writeBytes("No such file exists");  //Problematic
    }
    break;

1 Answer 1

0

A client-server program like this needs to be symmetric. If you write a line on one end, you must read a line on the other end. If you write 6 bytes, then the other end must read 6 bytes.

That's not the case here. The client sends a line:

outToServer.writeBytes(command + '\n');

And the server reads it:

String clientMsg = readIncoming.readLine();

Everything is fine until now. Then the client sends some bytes (a file name):

outToServer.writeBytes(file_name);

But the server reads a line:

String del_file = readIncoming.readLine();

So, the server will block until the client closes the socket or sends a newline character.

I'd strongly advise that you define a clear protocol, on paper, and that you use symmetric classes and methods on both ends.

3
  • Is it because the \n is included in the line outToServer.writeBytes(command + '\n'); that it does not create problems? Beacuse there too some bytes are sent (the command) and a line is read and stored in clientMsg. Also readIncoming is a BufferedReader and I did not find a method associated with a BufferedReader that reads bytes. Can you please suggest what should be used here instead of a BufferedReader?
    – 1xQ
    Jan 31, 2014 at 23:38
  • readLine() reads a whole line. The reader can't know if the line is complete until it sees the EOL character: \n. The rest is answered in the last part. If you use a Reader to read, then you should use a Writer to write. Not a DataOutputStream. If you use a DataOutputStream to write, then you should read using a DataInputStream.
    – JB Nizet
    Jan 31, 2014 at 23:42
  • I get the concept of symmetry now. And I could get the program to work. Thanks a lot man. Appreciate it.
    – 1xQ
    Feb 1, 2014 at 0:39

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.