简体   繁体   中英

BufferedReader in not throwing an exception after connection to socket ends

I use a bufferedReader to read output from a server in my client when the button is pressed, it works fine, however I can't get it to use method errorHandling when the connection closes - I wrap in(bufferedreader object).readLine in try/catch but it does NOT throw an exception, nor does out.checkError returns true when connection fails. The program just completely freezes and doesn't reach the last catch if connection ends.

        if(source==button) {
        String back = "";

        if(out.checkError()){
            errorHandling("I/O Closed");
            return;
        }

        switch(currentOp){
            case(0):
                out.println("R "+input.getText());
                break;
            case(1):
                out.println("P "+input.getText());
                break;
            case(2):
                out.println("M "+input.getText());
                break;
        }

        try{

            if((back=in.readLine())==null) throw new SocketServerException();
        }

        catch(Exception e){
            errorHandling("I/O Closed");
            return;
        }

The situation you describe is impossible. Ergo it is not happening. The peer socket must still be open. Check your observations.

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