0

I've got the most random problem. Simply put I look to iterate in a while loop until a certain condition is met and when its not met obviously the code after the loop should be run. For some reason the code after the while loop doesn't run... Here is my code:

while (true) 
        {
            Socket ClientSocketConnection = serverSocket.accept();
            System.err.println("We have established a connection with a client!");
            System.out.println(" ");

            ServerInput = new BufferedReader(new InputStreamReader(ClientSocketConnection.getInputStream()));
            ServerOutput = new DataOutputStream(ClientSocketConnection.getOutputStream());

            while((StringInput = ServerInput.read()) != -1)
            {
              //Things get done here

            }

            //methodBeingUsed is a string here

            switch (methodBeingUsed){
                case "GET":
                    GET();
                    break;
                case "POST":
                    POST(sBuffer.toString());
                    break;
            }

            System.err.println("The Connection will now Terminated!");
            ServerOutput.close();
            ServerInput.close();
            ClientSocketConnection.close();
        }
    }

Basically the switch statement isn't being run for some reason?? When I debugged the code I get to the last -1 from the ServerInput variable the code just stops. Stops and doesn't continue with everything else in the while(true) loop. Really not sure why this is happening...

3
  • 1
    What is MethodBeingUsed? What are the possible values it can have? Commented Oct 13, 2013 at 17:15
  • Oh sorry its just a String Variable Commented Oct 13, 2013 at 17:16
  • @user12345 : Where you able to get any solution for this? Commented Sep 2, 2016 at 19:49

1 Answer 1

3

This is obviously blocking method : ServerInput.read(), it means, that it waits until it actually reads something. If it does not read anything it is just waiting. You are probably not connected properly to the existining connection.

Sign up to request clarification or add additional context in comments.

3 Comments

Touche you are correct I thought at the end of the line of my data being read then a -1 would be used to show no more data is left and end the loop. Thank you!
Any idea of I can modify the code to kick out of the loop once the last bit of data has been read from the ServerInput variable?
I havent used Socket class for years... try to google "java non-blocking Socket read" and you can do it with it. Or something like "hasInput" boolean method should exists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.