我有一個應用程序(SWT),我需要在服務器端管理一個文件。通過管理我的意思是3件事情,寫入內容到文件中,應用讀寫鎖定機制和顯示相同在TextArea
。我需要創建一個多線程服務器來實現這一點,因爲我的應用程序(實際上是基於eclipse的插件)接受多個用戶。我不熟悉這種客戶端服務器和套接字編程,我已經閱讀了幾個教程,但仍未找到任何最佳解決方案。我不需要代碼(互聯網上有很多),我需要的方式或步驟來做到這一點。 謝謝。JAVA中的文件管理服務器
此外,我發現一些服務器代碼,實際上工作正常。但是不顯示所需的結果。
我想要用這個文件做的事情是維護作者的名字。 SVN在服務器端做的版本號和相關目標。
服務器程序:
public void onServer() throws Exception {
String sentByClient;
String line1 = null;
ServerSocket socket = new ServerSocket(6789);
while(true) {
System.out.println("Listening...");
Socket connectionSocket = socket.accept();
BufferedReader inFromClient =
new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
sentByClient = inFromClient.readLine();
System.out.println("Received: " + sentByClient);
File file=new File("HistoryFile.txt");//------------------server file
BufferedWriter writ=new BufferedWriter(new FileWriter(file));
writ.write(sentByClient);
writ.close();
BufferedReader read=new BufferedReader(new FileReader(file));
while((line1=read.readLine())!=null) {
System.out.println(line1);
}
outToClient.writeBytes(line1);
}
}
客戶端代碼:
public void onClient(String param) throws Exception {
Socket clientSocket = new Socket("localhost", 6789);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
//sentence = inFromUser.readLine();
sentence=param; // Here i'm sending the author name, revision details, etc from svn to my server
outToServer.writeBytes(sentence);
newSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + newSentence);
historyArea.append(newSentence);
}
我其實需要的是保持服務器上的文件和文本區域(historyArea)顯示文件內容。我將歷史數據從SVN發送到文件。
期望Ouptut:
Revision Number: 1
Author: a
Time:xxxx
Changed Path:xxxx
-------------------
Revision Number: 2
Author: a
Time:xxxx
Changed Path:xxxx
------------------
Revision Number: 3
Author: a
Time:xxxx
Changed Path:xxxx
Ouptut我得到僅僅是第一次修訂:
Revision Number: 1
Author: a
Time:xxxx
Changed Path:xxxx
在onClient()中,輸出'param'並檢查你是否確實發送了正確的數據。我認爲問題出在調用onClient()的代碼中。 – Kishore 2013-04-18 08:45:48
@NandakishoreK是的,可能是這個原因,正如我在代碼中提到的那樣,它打印服務器發送的數據,輸出中沒有這樣的事情(在客戶端),必須重新檢查該方法。 – 2013-04-18 09:00:42