简体   繁体   English

Java中的套接字的BufferedReader.readLine()卡住了

[英]BufferedReader.readLine() from a socket in Java stuck

I will try to explain my problem in detail. 我将尝试详细解释我的问题。 Feel free to ask if you need more details. 随时询问您是否需要更多详细信息。

I have a multi-client/server connection threaded by Java sockets. 我有一个由Java套接字线程化的多客户机/服务器连接。 All the exchanges operate according to the protocol on a simple case by case basis. 所有交换均在简单的情况下根据协议进行操作。 The method searchFlight(destination,date) queries the the SQL database (sorry I can't provide it) and returns a ResultSet variable. 方法searchFlight(destination,date)查询SQL数据库(很抱歉,我无法提供它)并返回ResultSet变量。 And then, the method displaySelected(ResultSet) sends the ResultSet as a String to the client line by line. 然后,方法displaySelected(ResultSet)将ResultSet作为String逐行发送到客户端。

The communication works well until the server sends the "Hour" to the client. 在服务器将“小时”发送给客户端之前,通信正常进行。 I monitored the values on the server and it appears that the Server sends the correct String to the client (which is supposed to be "Flight hour (HH:MM):") but the client only prints the previous one. 我监视了服务器上的值,服务器似乎向客户端发送了正确的字符串(应该是“飞行时间(HH:MM):”),但是客户端仅打印了前一个。 Concretely, it is stuck like this: 具体来说,它像这样被卡住:

(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#):

I am stuck with this problem for a couple of days now, and I really don't know how to fix it. 我被这个问题困扰了几天,而且我真的不知道如何解决它。 I have tried a lot of alternatives without success. 我尝试了很多替代方法,但均未成功。 I hope someone more experienced with Java than me could help me. 我希望有人比我更有经验,可以帮助我。

Thank you in advance. 先感谢您。

You can see all my code bellow. 您可以在下面看到我的所有代码。


CLIENT SIDE 客户端

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();
        }
    }
}

Here is the issue. 这是问题。 On the client, when you're reading the flight/schedule list, for every line of display only data you read from the server, you're sending the previous input that the user entered to the server. 在客户端上,当您阅读航班/时间表列表时,对于仅显示从服务器读取的数据的每一行,您都将用户输入的先前输入发送到服务器。 That's the reason it keeps printing City , because the client keeps on sending 2 that it collected for the option encampment or city . 这是它保持印刷的原因City ,因为客户端不断发送2 ,它收集的选项encampment or city

Replace 更换

if(!fromServer.endsWith("  ")){
    fromClient = stdIn.readLine();
} 

with

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;
} 

Apart from this, you need to handle the Hour input in your Protocol.process(..) 除此之外,您还需要处理Protocol.process(..)中的Protocol.process(..) Hour输入Protocol.process(..)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM