缓冲读取器采用终端输入字符



我正在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.212n220 (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 UNIXnUse binary mode to transfer filesnftp> ");
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的行被跳过。根据堆栈溢出的答案,我的输入应该以终止字符结尾。我不知道该怎么做。

我假设"跳过"是指"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());

最新更新