我试图将服务器置于“等待模式”,但是当我使用以下语法时:
new Thread() {
public void run() {
BufferedReader in;
String op;
try {
in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
while ((op = in.readLine()) != null) {
if (op.equals("Elimina")) {
String tmp = in.readLine();
File file = new File("src/server/" + nomeAccount + "/" + tmp + ".txt");
file.delete();
}
}
} catch (IOException ex) {
System.out.println("Unable to read messages");
} finally {
try {
incoming.close();
} catch (IOException ex) {
System.out.println("Cannot close the socket");
}
}
}
}.start();
该程序卡在while ((op = in.readLine()) != null)
上,并且客户端从不加载其GUI(如果不加载,我将无法进行任何输入)。因此,我决定使用以下语法:
new Thread() {
@Override
public void run() {
BufferedReader in;
String op;
try {
in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
while (in.ready()) {
op = in.readLine();
if (op.equals("Elimina")) {
String tmp = in.readLine();
contenutoTextArea.append("Ho eliminato la mail " + tmp + " \n");
textarea.setText(contenutoTextArea.toString());
File file = new File("src/server/" + nomeAccount + "/" + tmp + ".txt");
file.delete();
}
}
System.out.println("bbbbb");
} catch (IOException ex) {
System.out.println("Unable to read messages");
} finally {
try {
incoming.close();
} catch (IOException ex) {
System.out.println("Cannot close the socket");
}
}
}
}.start();
现在的问题是该程序永远不会进入循环。在此之前,我尝试使用while (true)
,但程序再次卡住。