Java клиент передачи файлов и сервер не работают синхронно - PullRequest
0 голосов
/ 24 февраля 2020

Я работаю над программой передачи файлов между клиентом и сервером в Java. После того, как они подключены, сервер отправляет клиенту список опций - случай переключения в течение некоторого времени l oop - прежде чем перейти к передаче. У меня проблемы с совместным использованием двух программ. Вторая опция, которую вводит пользователь, отправляется на сервер, как если бы это был файл, а не регистр в операторе switch.

Вот код для серверной части:

String cmd;

            do {
            dos.writeUTF("For the user dictionary, type 1 \n" +
                    "For examples of transfer commands, type 2 \n" +
                    "For examples of directory and file commands, type 3 \n" +
                    "For more options type HELP \n" +
                    "Type ESC to begin");

            cmd = dis.readUTF();

            if (cmd.equals("ESC")) {
                break;
            }

            switch(cmd) {
                case "1" :
                    dos.writeUTF("some text\n");

                case "2" : 
                    dos.writeUTF("more text\n");

                case "3" :
                    dos.writeUTF("even more\n");

                case "HELP" :
                    dos.writeUTF("Sorry, there's no helping you");

                case "ESC" :
                    break;

            } // end switch
            }while(!cmd.equals("ESC"));
            // end do while

            // authentication accepted; enters connected state
            dos.writeUTF("Enter a command to begin a transfer: ");


            // read 
            int bytesRead;
            int current = 0;
            InputStream in = s.getInputStream();

            // Writing the file to disk
            // Instantiating a new output stream object
            OutputStream output = new FileOutputStream(dis.readUTF());

            byte[] buffer = new byte[1024];
            while ((bytesRead = in.read(buffer)) != -1) {
                output.write(buffer, 0, bytesRead);
            }
            // Closing the FileOutputStream handle
            output.close();

Вот клиент:

// User Dictionary
         do{
                System.out.println(dis.readUTF()); 
                tosend = scn.nextLine(); 
                dos.writeUTF(tosend);

                if(tosend.equals("END")) 
                { 
                    s.close(); 
                    System.out.println("Connection closed"); 
                    break;
                }

                if(tosend.equals("ESC")) {
                    break;
                }
        }while (!tosend.equals("ESC"));
         // end do while loop



        // send transfer
        System.out.println("Enter transfer information: "); 
        File myFile = new File(scn.nextLine());

        //File myFile = new File("Untitled.txt");
        byte[] mybytearray = new byte[(int) myFile.length()];

        FileInputStream fis = new FileInputStream(myFile);
        BufferedInputStream bis = new BufferedInputStream(fis);
        bis.read(mybytearray, 0, mybytearray.length);

        OutputStream os = s.getOutputStream();

        os.write(mybytearray, 0, mybytearray.length);

        System.out.println("You have successfully transferred" + myFile);
...