BufferedReader 接受终端输入字符

BufferedReader taking terminal enter character

我正在 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 的行将被跳过。根据 Whosebug 上的回答,我的输入应该以终止符结尾。我不知道该怎么做。

我假设 "skipped" 是指单词 'exit' 打印在下一行,而不是像前几行那样直接打印在 "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());