Paul Statham wrote:
What causes request.read() to return -1? Is it because the PrintWriter has been flushed and there's nothing left to read, or is it because clientSocket.close() has been called?
It's the close(). Flush doesn't mean that no more will be coming, only that whatever's currently buffered should be pushed through.
Also, I don't think you're supposed to use PrintWriter with sockets. In your particular case, it wouldn't matter, but if I recall correctly, there can be cases where a mismatch between line-end conventions between client and server can lead to deadlock, or at least severe slow-downs.
Also, in general, casting (char)byte is a bad idea. It's fine for the lower ASCII 127, and maybe for all of ASCII, but it won't work in the general case of multi-byte characters. Rather than reading a byte at a time, read into a buffer. And if you're sending text, then rather than converting byte to char yourself, use a Reader to do it for you.