File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Socket input/output streams

 
Paul Statham
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thank you, very helpful
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic