我正在嘗試編寫一個基本的客戶端服務器程序,以允許客戶端遍歷服務器的文件。我希望服務器在連接客戶端時將當前目錄中所有文件的列表發送給客戶端。我會如何去做這件事?我已經創建了一個包含所有文件名的數組,並且正試圖將這些文件發送給客戶端,它只是在一個無限循環中(因爲它意味着!)並且什麼都不做。如何讓服務器向客戶端發送連接消息?
試圖在服務器上的客戶端的連接發送的消息:
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
boolean found = false;
//Read the data from the input stream
for (int i=0;i < fileList.length;i++) {
outToClient.writeBytes(fileList[i].getName());
}
並且讓服務器接收這樣的:
//Prepare an object for reading from the input stream
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
//Read the data from the input stream
sentence = inFromServer.readLine();
,但這樣做只是讓客戶靜坐在無限循環,什麼都不做。
請幫忙嗎?
我試圖讓服務器立即發送連接,但它不會工作。我將編輯我的帖子以顯示我現在正在嘗試的內容。 – 2011-03-10 15:52:36
@John McCarthy - 新增示例代碼來回答。 – 2011-03-10 16:48:19