简体   繁体   中英

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. 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. And then, the method displaySelected(ResultSet) sends the ResultSet as a String to the client line by line.

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. 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 .

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(..)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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