如何分离输入流 - 简单的客户端套接字程序

时间:2018-02-27 11:31:06

标签: java sockets tcp ftp client-server

客户端:

public class Client {

    private Socket clientSocket;
    private PrintWriter out;
    private BufferedReader in;
    private OutputStream os;
    private BufferedInputStream bis;
    private FileInputStream fis;
    private FileOutputStream fos;
    private BufferedOutputStream bos;
    private int bytesRead;
    private int current;

    public Client(String host, int port) throws IOException {
        clientSocket = new Socket(host, port);
        out = new PrintWriter(clientSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    }

    public void stopConnection() throws IOException {
        in.close();
        out.close();
        clientSocket.close();
    }

    public void menu() throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            System.out.println("********************");
            System.out.println("1. CONN: Connect to Server");
            System.out.println("2. UPLD: Upload file");
            System.out.println("********************");
            System.out.print("Enter Choice: ");
            String input = br.readLine();
            out.write(input);
            out.flush();
            if(input.equals("UPLD")){
                sendFile();
            }
        }
    }

    public void sendFile() throws IOException {
        System.out.println("Enter filename");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String fileName = br.readLine();
        File myFile = new File(fileName);
        if(!myFile.exists()){
            System.out.println("File doesn't exist!");
            return;
        }
        else{
            System.out.println("File has been found!");
        }
        DataOutputStream dos = new DataOutputStream(clientSocket.getOutputStream()); //get the output stream of the socket
        dos.writeInt((int) myFile.length()); //write in the length of the file
        InputStream in = new FileInputStream(myFile); //create an inputstream from the file
        OutputStream out = clientSocket.getOutputStream(); //get output stream
        byte[] buf = new byte[8192]; //create buffer
        int len = 0;
        while ((len = in.read(buf)) != -1) {
            out.write(buf, 0, len); //write buffer
        }
        in.close(); //clean up
        out.close();
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter Host:");
        String h = br.readLine();
        System.out.print("Enter port:");
        String p = br.readLine();
        Client ftp = new Client(h,Integer.parseInt(p));
        ftp.menu();
    }
} 

服务器:

public class Server {
    private ServerSocket serverSocket;
    private Socket clientSocket;
    private PrintWriter out;
    private BufferedReader in;
    private OutputStream os;
    private BufferedInputStream bis;
    private FileInputStream fis;

    private String fileName = "SampleFile.txt";

    public void start(int port) throws IOException {
        serverSocket = new ServerSocket(port);
        while (true) {
            String input;
            clientSocket = serverSocket.accept();
            out = new PrintWriter(clientSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

            input = in.readLine();
            if (input.equals("UPLD")) {
                //System.out.println("activate uploadFile");
                //System.out.println(input);
                receiveFile();
            } else System.out.println("Why?");
        }
    }

    public void stop() throws IOException {
        in.close();
        out.close();
        clientSocket.close();
        serverSocket.close();
    }

    public void receiveFile() throws IOException {
        DataInputStream dis = new DataInputStream(clientSocket.getInputStream()); //get the socket's input stream
        int size = dis.readInt(); //get the size of the file.
        InputStream inp = clientSocket.getInputStream();
        OutputStream out = new FileOutputStream("copy.txt"); //stream to write out file
        int totalBytesRead = 0;
        byte[] buf = new byte[8192]; //buffer
        int len = 0;
        while ((len = inp.read(buf)) != -1) {
            out.write(buf, 0, len); //write buffer
        }
        out.close(); //clean up
        inp.close();
    }

    public static void main(String[] args) throws IOException {
        Server server = new Server();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Please enter the port you wish to bind to: ");
        String port = br.readLine();
        server.start(Integer.parseInt(port));
        System.out.println("Connected to client; awaiting operation request");
    }
} 

如何使通过br.readline()分配给String输入的客户端输入首先发送到服务器,以便服务器知道调用receiveFile()方法,然后sendFile()方法是在客户端打电话。我完全不知道为什么它现在不工作 - 我在输入写入PrintWriter后执行flush(),那么为什么服务器不能先收到它,然后接收文件呢?目前,当我打印服务器的BufferedReader时,它在同一行上有输入和文件内容。

1 个答案:

答案 0 :(得分:0)

它的nextline char在客户端出现问题。 附加UPLD \ n&写下来。 您正在读取input = in.readLine(); @server side

public void menu() throws IOException{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    while (true) {
        System.out.println("********************");
        System.out.println("1. CONN: Connect to Server");
        System.out.println("2. UPLD: Upload file");
        System.out.println("********************");
        System.out.print("Enter Choice: ");
        String input = br.readLine();
        out.write(input+"\n");
        out.flush();
        if(input.equals("UPLD")){
            sendFile();
        }
    }
}