This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Sending Image to Client" Watch "Sending Image to Client" New topic
Author

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
Bartender

Joined: Oct 29, 2001
Posts: 8834
    
    7

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
 
 
subject: Sending Image to Client
 
Similar Threads
I need help improving this chat server and chat client!
How to append response to the incoming request from Socket and write to another Socket.....
sending an encrypted byte array through a socket
Sending multiple images
Ain't constructin', ain't givin' me an error...