我会尽力详细解释我的问题。如果您需要更多详细信息,请随时询问。
我有一个由 Java 套接字线程化的多客户端/服务器连接。所有交易所都根据简单的具体情况按照协议(protocol)进行操作。 searchFlight(destination,date) 方法查询 SQL 数据库(抱歉我无法提供)并返回 ResultSet 变量。然后,displaySelected(ResultSet)方法将ResultSet以字符串形式逐行发送到客户端。
在服务器将“小时”发送给客户端之前,通信工作正常。我监视服务器上的值,看来服务器向客户端发送了正确的字符串(应该是“飞行时间(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#):
我被这个问题困扰了几天,我真的不知道如何解决它。 我尝试了很多替代方案但没有成功。我希望比我更有 Java 经验的人可以帮助我。
提前谢谢您。
您可以在下面看到我的所有代码。
<小时/>客户端
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
的原因,因为客户端不断发送它为 encampment 或 city
选项收集的 2
。
替换
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(..)
中的 Hour
输入
关于java - Java 中套接字的 BufferedReader.readLine() 卡住了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8186758/