I am running a client and servlet under jetty locally. When I read the message in the client, I do:

```
in = new Scanner(conn.getInputStream());
StringBuffer messageBuffer = new StringBuffer();
while (in.hasNext()) {
   messageBuffer.append(in.next()).append(" ");
}

```

and I expect that when there is no data coming from the servlet, it should freeze at

```
while (in.hasNext())

```

instead, I just end up with empty messageBuffer, and i have to deal with it and call the method again and again until I get a message. Why is this happening? How can I make it stop at the while statement and wait until there is data coming in?

Here is how the url connection is started(once, 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();
    }

```