| Author |
Socket input/output streams
|
Paul Statham
Ranch Hand
Joined: Dec 05, 2008
Posts: 38
|
|
Can someone clarify to me that I'm understanding this correctly.
In my client program I have something like this
Then in the server code I have
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?
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5895
|
|
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.
|
 |
Paul Statham
Ranch Hand
Joined: Dec 05, 2008
Posts: 38
|
|
Ok thank you, very helpful
|
 |
 |
|
|
subject: Socket input/output streams
|
|
|