Meaningless Drivel is fun!
The moose likes I/O and Streams and the fly likes Sending Image to Client Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Sending Image to Client" Watch "Sending Image to Client" New topic

Sending Image to Client

Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Readers,
I need to send an image from the Server to the Client. I have successfully done so but how do I inform the Client that the entire image has been sent.
I use BufferedOutputStream on the Server side to send data to the client by reading the image file with the help of FileInputStream's read() method.
I tried sending "-1" as a marker to inform the client that the entire image has been sent. Unfortunately, since the write() method of BufferedOutputStream sends only "byte" data, the "-1" gets truncated to "255".
Because of this, the Client waits for more input from the Server leaving my program stranded after that.

With Best Regards,
Shyam Prasad Murarka
Joe Ess

Joined: Oct 29, 2001
Posts: 9190

You are thinking about a protocol, a wrapper around the data so your server and client can converse.
The simplest options off the top of my head:
-close the stream when you are done writing data, then the receiving end gets an EOF
-if you want to keep the socket open for a prolonged conversation, use the Object*putStreams to read/write Java objects
- another option is to precede any data with a value indicating the length of the message. If you are transmitting various kinds of data you could also include a value indicating which kind of data or even what kind of message you are sending.

[How To Ask Questions On JavaRanch]
I agree. Here's the link:
subject: Sending Image to Client
It's not a secret anymore!