我需要一種方法來自動關閉通過使用某種計時器運行的服務器程序。直到現在這是我得到的如何自動關閉Java服務器?
long start = System.currentTimeMillis();
long end = start + 10 * 1000; // 60 seconds * 1000 ms/sec
while (System.currentTimeMillis() < end) {
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(
new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(
connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
System.out.println("Received: " + clientSentence);
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
count = count + 1;
}
welcomeSocket.close();
這可能嗎?請幫忙,不要投票。我很喜歡Java中的客戶端服務器。