aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Socket input/output streams Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Socket input/output streams" Watch "Socket input/output streams" New topic
Author

Socket input/output streams

Paul Statham
Ranch Hand

Joined: Dec 05, 2008
Posts: 40
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: 6109
    
    6

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: 40
Ok thank you, very helpful
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Socket input/output streams
 
Similar Threads
Socket : MultiClient Server
Reading a binary file from a client to server
Socket scenario missing data
Doubt in ServerSocket and Socket
J2se server and J2me client