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.
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.