简体   繁体   中英

Dealing with EOFException in java

I have the following function. I want the loop to continue until the message equals "You have successfully logged in!". But when the user enters a wrong input (invalid username or password) then the loop goes on infinitely, because of an EOFException . How can I solve this? Thank you.

 private void processConnection() throws IOException
    {
        boolean eofReached = false;
        String message;
        do
        {
            try 
            {
                message = (String)input.readObject();
                if(message.equals("You have successfully logged in!"))
                {
                    displayMessage(String.format("\n%s ", message));
                    logged = true;
                    prepareUI();
                } 
                else
                {
                    displayMessage(String.format("\n%s %s", message, "\nTry again!") );
                }
            } 
            catch (ClassNotFoundException | EOFException  ex) 
            {
                  System.out.printf("\nend of registering");
                //  Logger.getLogger(ClientJFrame.class.getName()).log(Level.SEVERE, null, ex);
                  //eofReached = true;
            }        
             cout("registering");       
        } while(!logged && !eofReached);
    }

In your code sample, it looks like you have everything you should need. Just un-comment the line where you're setting eofReached .

Well I found my mistake. The problem was that there was no such corresponding loop in the server to answer all of the requests. that's why I was getting the exception.

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