BufferedReader带终端输入字符

时间:2018-09-12 19:53:43

标签: java input bufferedreader

我正在用Java实现套接字编程,其中我使用BufferedReader从客户端获取输入。但是,BufferedReader对象采用在控制台上输入的换行符。 这是我的服务器端代码:

import java.net.*;
import java.io.*;

class FTPserver {

private ServerSocket serverSocket = null;
private DataInputStream dis = null;
private DataOutputStream dos = null;

FTPserver() {
    try {
        String input;
        serverSocket = new ServerSocket(3000);
        Socket socket = null;
        socket = serverSocket.accept();
        dis = new DataInputStream(socket.getInputStream());
        dos = new DataOutputStream(socket.getOutputStream());
        input = dis.readUTF();
        if(input.equals("ftp")) {
            dos.writeUTF("ftp> ");
            input = dis.readUTF();
            System.out.print("opened connection to 10.10.10.212");
            dos.writeUTF("Connected to 10.10.10.212\n220 (vsFTPd 3.0.2)\nName (10.10.10.212:root): ");
            input = dis.readUTF();
            dos.writeUTF("331 Please specify the password.\nPassword: ");
            input = dis.readUTF();
            dos.writeUTF("230 Login successful.\nRemote system type is UNIX\nUse binary mode to transfer files\nftp> ");
            input = dis.readUTF();       //receive mget
            dos.writeUTF("ftp> ");
            input = dis.readUTF();       //receive mput
            dos.writeUTF("ftp> ");
            //input = dis.readUTF();
            input = dis.readUTF();       //receive exit
            dos.writeUTF("Goodbye");
            input = dis.readUTF();       //receive exit
            dos.writeUTF("Goodbye");
        }
        dis.close();
        dos.close();
        socket.close();
    }
    catch(Exception e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    FTPserver ftp = new FTPserver();
}
}

这是我的客户代码:

import java.net.*;
import java.io.*;
import java.util.Scanner;

class FTPclient {
private DataInputStream dis = null;
private DataOutputStream dos = null;
private Socket socket = null;

FTPclient() {
    try {
        String input,output;
        BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
        socket = new Socket("localhost",3000);
        dis = new DataInputStream(socket.getInputStream());
        dos = new DataOutputStream(socket.getOutputStream());
        output = sc.readLine();
        dos.writeUTF(output);               // write ftp
        System.out.print(dis.readUTF());    // print ftp>
        dos.writeUTF(sc.readLine());            // write open 10.10.10.212
        System.out.print(dis.readUTF());    // print connected
        dos.writeUTF(sc.readLine());
        System.out.print(dis.readUTF());
        dos.writeUTF(sc.readLine());             //send mget
        System.out.print(dis.readUTF());

        dos.writeUTF(sc.readLine());             //send mput
        System.out.print(dis.readUTF());
        dos.writeUTF(sc.readLine());            //send exit
        System.out.println(dis.readUTF());
        dos.writeUTF(sc.readLine());             //send exit
        System.out.println(dis.readUTF());
        dis.close();
        dos.close();
        socket.close();
    }
    catch(Exception e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    FTPclient ftp = new FTPclient();
}
}

这是我在客户端控制台上输入的内容:

dell@dell-Inspiron-15-3567:~$ java FTPclient
ftp
ftp> open 10.10.10.212
Connected to 10.10.10.212
220 (vsFTPd 3.0.2)
Name (10.10.10.212:root): student
331 Please specify the password.
Password: student
230 Login successful.
Remote system type is UNIX
Use binary mode to transfer files
ftp> mget *.py
ftp> mput sample.java
ftp>                   //this line is getting skipped
exit
Goodbye
dell@dell-Inspiron-15-3567:~$

如上面的控制台代码段所述,用户输入exit的行将被跳过。根据关于stackoverflow的回答,我的输入应以终止字符结尾。我不知道该怎么做。

1 个答案:

答案 0 :(得分:0)

我假设“跳过”是指在下一行而不是像在前几行中的“ ftp>”之后直接打印“退出”一词。如果是这样,我认为发生这种情况的原因是由于您在客户端的这些行中使用System.out.println而不是您在前几行中使用System.out.print。我认为您的输入正确终止了,因为您的程序似乎正确结束了,并且没有继续等待进一步的输入。

    dos.writeUTF(sc.readLine());            //send exit
    System.out.println(dis.readUTF());
    dos.writeUTF(sc.readLine());             //send exit
    System.out.println(dis.readUTF());