我的问题是我正在创建一个 FTP 客户端,到目前为止,除了一个小细节之外,它的工作完美无缺,这一直困扰着我。我需要知道 FTP 欢迎消息跨越了多少行......这是不能接受的!
private Socket connection;
private PrintWriter outStream;
private Scanner inStream;
public void InitiateConnection() throws IOException
{
log.Info(this, "Initiating connection to host: " + host + ":" + port);
connection = new Socket(host, port);
log.Info(this, "Connection initiated.");
outStream = new PrintWriter(connection.getOutputStream(), true);
inStream = new Scanner(connection.getInputStream());
Listen();
Listen();
Listen();
}
public String Listen() throws IOException
{
if(connection == null)
throw new IOException("Connection not initiated yet");
String response = inStream.nextLine();
log.Info(this, "Response: " + response);
return response;
}
这是简单的设置,我省略了所有其他代码,因为它与我的问题没有任何关系。
我尝试了多种方法来实现这一目标。失败的解决方案1:
String response = "";
while(response != null)
Listen();
失败的解决方案2:
while(connection.getInputStream().available > 0)
Listen();
还有无数其他的......但要么它不起作用,要么方法阻塞并等待新的输入。我什至尝试过超时,但这也不能完美地工作,它不是解决这个问题的正确方法......
I need to be able to get the entire welcome message from the FTP server, without knowing the amount of lines... So I can both get this:
Response: 220-FileZilla Server version 0.9.39 beta
Response: 220-written by Tim Kosse (Tim.Kosse@gmx.de)
Response: 220 Please visit http://sourceforge.net/projects/filezilla/
And this:
Response: 220-FileZilla Server version 0.9.40 beta
Response: 220 Welcome to Andrés FTP Server