![](/img/trans.png)
[英]Java Socket - how to catch Exception of BufferedReader.readline()
[英]BufferedReader.readLine() from a socket in Java stuck
我將嘗試詳細解釋我的問題。 隨時詢問您是否需要更多詳細信息。
我有一個由Java套接字線程化的多客戶機/服務器連接。 所有交換均在簡單的情況下根據協議進行操作。 方法searchFlight(destination,date)查詢SQL數據庫(很抱歉,我無法提供它)並返回ResultSet變量。 然后,方法displaySelected(ResultSet)將ResultSet作為String逐行發送到客戶端。
在服務器將“小時”發送給客戶端之前,通信正常進行。 我監視了服務器上的值,服務器似乎向客戶端發送了正確的字符串(應該是“飛行時間(HH:MM):”),但是客戶端僅打印了前一個。 具體來說,它像這樣被卡住:
(1) Make reservation | (2) Cancel reservation
1
Choose the departure date (YYYY-MM-DD):
2012-01-01
(1) Go to Encampment | (2) Go to City:
2
+------|-----------|------------|-------------|----------|-------+
| Code | Company | Dest | Date | Hour | Seats |
+------+-----------+------------+-------------+----------+-------+
| AER2 | Aerocamp | City | 2012-01-01 | 07:00:00 | 5 /6 |
| COP2 | CopterFly | City | 2012-01-01 | 09:00:00 | 5 /6 |
| AER1 | Aerocamp | City | 2012-01-01 | 10:00:00 | 3 /6 |
| H001 | HeliAir | City | 2012-01-01 | 11:00:00 | 6 /6 |
| COP1 | CopterFly | City | 2012-01-01 | 11:00:00 | 6 /6 |
| AER2 | Aerocamp | City | 2012-01-01 | 13:00:00 | 4 /6 |
| COP2 | CopterFly | City | 2012-01-01 | 15:00:00 | 2 /6 |
| AER1 | Aerocamp | City | 2012-01-01 | 16:00:00 | 6 /6 |
| COP1 | CopterFly | City | 2012-01-01 | 17:00:00 | 2 /6 |
| COP3 | CopterFly | City | 2012-01-01 | 20:00:00 | 3 /6 |
+------|-----------|------------|-------------|----------|-------+
Flight code (XXX#):
AER1
Flight code (XXX#):
Flight code (XXX#):
我被這個問題困擾了幾天,而且我真的不知道如何解決它。 我嘗試了很多替代方法,但均未成功。 我希望有人比我更有經驗,可以幫助我。
先感謝您。
您可以在下面看到我的所有代碼。
客戶端
public class Client {
static Socket clientSocket = null;
public static void main(String[]args) throws IOException{
PrintWriter out = null;
BufferedReader in = null;
try {
clientSocket = new Socket("localhost", 1024);
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host:");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection.");
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromClient = null;
try {
while (((fromServer = in.readLine()) != null)) {
System.out.println(fromServer);
System.out.flush();
if(!fromServer.endsWith(" ")){
fromClient = stdIn.readLine();
}
if (fromClient != null) {
out.println(fromClient);
out.flush();
}
}
out.close();
in.close();
stdIn.close();
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
這是問題。 在客戶端上,當您閱讀航班/時間表列表時,對於僅顯示從服務器讀取的數據的每一行,您都將用戶輸入的先前輸入發送到服務器。 這是它保持印刷的原因City
,因為客戶端不斷發送2
,它收集的選項encampment or city
。
更換
if(!fromServer.endsWith(" ")){
fromClient = stdIn.readLine();
}
同
if(!fromServer.endsWith(" ")){
fromClient = stdIn.readLine();
} else {
// Data from the server is for display only. No input is required.
// Clear out previous input.
fromClient = null;
}
除此之外,您還需要處理Protocol.process(..)
中的Protocol.process(..)
Hour
輸入Protocol.process(..)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.