java - 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 上的答案,我的输入应该以终止字符结尾。我不知道该怎么做。
解决方案
我假设“跳过”是指“退出”这个词被打印在下一行,而不是像前面几行那样直接打印在“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());
推荐阅读
- javascript - 在javascript中查找和替换单词后的子字符串
- amazon-ec2 - hazelcast-jet 部署和数据摄取
- php - SEE 服务器在屏幕共享 RTCMultiConnection 时无法与加入房间一起使用
- android - 在不同的包中提供相同的类?
- cvxpy - CVXPY 试图制定大问题:ValueError:negative dimensions are not allowed。RAM使用是这里的问题吗?
- mysql - 将数据从 MySQL 加载到 HDFS 时 Sqoop 导入错误
- spring-boot - Spring Aspect - 如何识别哪个 Pointcut 触发了函数
- c# - 使用 Datatable 和 MVVM 的 WPF 示例
- json - 我无法在 shell 中使用 jq 从 JSON 文件中获取 Key 属性值
- android - 如何更改字符串显示