0

On Servlet side:

for (GameParticipant activePlayer : connector.activePlayers) {
            activePlayer.out.println(response);
            activePlayer.out.flush();
            System.out.println("Server sending board state to all game participants:" + response);

(activePlayer.out is a PrintWriter saved in the server from the HttpResponse object obtained when that client connected the first time)

On clinet side:

private void receiveMessageFromServer() {
    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String input = null;
        while ((input = br.readLine()) != null){
            sb.append(input).append(" ");
        }
        }

For some reason, this communication works only the first time, when the client requests connection and waits for response in the same method, while the server uses the PrintWriter obtained directly fron the available HttpRespnse in the doPost method. After that, when the servlet tries to reuse the PrintWriter to talk to the clinet outside of a doPost method, nothing happens, the message never gets to the client. Any ideas?

P.S. In client constructor:

try {
        url = new URL("http://localhost:8182/stream");
        conn = (HttpURLConnection) url.openConnection();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException ioE) {
        ioE.printStackTrace();
    }
2
  • Posting the entire code instead of random pieces would make things more clear. Also, "when the client requests connection and waits for response in the same method", do you mean the same client, or a second, new client? Commented Aug 19, 2012 at 0:34
  • The same client, and in the same method where it makes the request for connection, it also awaits the response. I think the problem has to do with reusing URL and connections Commented Aug 19, 2012 at 1:53

1 Answer 1

1

The response output stream isn't valid outside the doPost() method, or more properly speaking the service() method. It can only be used to send one response. However PrintWriter swallows exceptions, as you will find when you check its error status, so you didn't see the problem.

In other words your entire server-side design is flawed. You can't misuse the Servlet Specification in that way.

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

Comments

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.