【问题标题】:Issues with run() methodrun() 方法的问题
【发布时间】:2016-04-15 23:40:52
【问题描述】:

描述:这是一个基于 TCP 套接字的 FTP 程序的 FTPClient.java 类。我的教授说我的程序很好,除了 FTPClient.java 类中的 run() 方法。他说我的 run() 方法是读取所有内容而不是读取特定命令。

问题:我应该在这个方法中使用 String[] output = something.split("") 吗?你能帮我找出这个方法的正确代码吗?

谢谢!

package ftp.server;
import java.net.*;
import java.io.*;

public class FTPClient
{
        private Socket socket = null;
        private DataInputStream input = null;
        private DataOutputStream output = null;
        private BufferedReader keyboard = null;
        final private String EXIT_CMD = "exit";
        final private String PUT_CMD = "put";
        final private String GET_CMD = "get";


        public FTPClient(Socket socket) throws IOException
        {
                this.socket = socket;
                input = new DataInputStream(socket.getInputStream());
                output = new DataOutputStream(socket.getOutputStream());
                keyboard = new BufferedReader(
                        new InputStreamReader(socket.getInputStream()));
        }

        public void sendFile(String fileName) throws IOException
        {
                File file = new File(fileName);
                if(!file.exists())
                {
                        output.writeUTF("File " + fileName + " does not  exist");
                }
                else
                {
                        output.writeUTF(fileName);
                        FileInputStream inFile =
                                new FileInputStream(file);
                        int ch;
                        do
                        {
                            ch = inFile.read();
                            output.writeUTF(String.valueOf(ch));
                    }while(ch != -1);
                    inFile.close();
            }
    }

    public void receiveFile(String fileName) throws IOException
    {
            output.writeUTF(fileName);
            String msg = input.readUTF();

            if(msg.equals("NOT_FOUND"))
            {
                    output.writeUTF("No file in server");
            }
            else if(msg.equals("READY"))
            {
                    File file = new File(fileName);
                    FileOutputStream outFile =
                            new FileOutputStream(file);
                    int ch;
                    String line;
                    do
                    {
                            line = input.readUTF();
                            ch = Integer.parseInt(line);
                            if(ch != -1)
                            {
                                    outFile.write(ch);
                            }
                    }while(ch != -1);
                    outFile.close();
            }
    }
    public void run() throws IOException
    {
            while(true)
            {
                    System.out.println("ftp>");
                    String cmd = keyboard.readLine();
                    if(cmd.equals(PUT_CMD))
                    {
                            output.writeUTF(PUT_CMD);
                            sendFile("novel.dat");
                    }
                    if(cmd.equals(GET_CMD))
                    {
                            output.writeUTF(GET_CMD);
                            receiveFile("novel.dat");
                    }
                    if(cmd.equals(EXIT_CMD))
                    {
                            output.writeUTF(EXIT_CMD);
                    }
            }
    }

    public static void main(String[] args) throws Exception
    {
            //check parameters
            if(args.length != 2)
            {
                    System.out.println("Usage: java FTPClient <host> <port>");
                    return;
            }

            //create socket
            String host = args[0];
            int port = Integer.parseInt(args[1]);
            Socket socket = new Socket(host, port);

            //run ftp client
            FTPClient client = new FTPClient(socket);
            client.run();
    }
}

【问题讨论】:

    标签: ftp ftp-client


    【解决方案1】:

    这是我需要做的来修复我的运行方法:

    public void run() throws IOException
        {
                while(true)
                {
                        System.out.print("ftp>");
                        String cmd = keyboard.readLine();
    
                        if(cmd.equals(EXIT_CMD))
                        {
                                output.writeUTF(EXIT_CMD);
                    System.exit(0);
    
                        }
    
                String[] tokens = cmd.split(" ");
                cmd = tokens[0];
                        String fileName = tokens[1];
    
                        if(cmd.equals(PUT_CMD))
                        {
                                output.writeUTF(PUT_CMD);
                                sendFile(fileName);
                        }
                        else if(cmd.equals(GET_CMD))
                        {
                                output.writeUTF(GET_CMD);
                                receiveFile(fileName);
                        }
                }
        }
    

    【讨论】:

      猜你喜欢
      • 2014-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-25
      • 2013-11-10
      • 2021-12-05
      • 1970-01-01
      相关资源
      最近更新 更多