Using sockets, I guess you'd serialise your image representation into a byte stream and write those bytes to the socket. Possibly (i.e. untested) something like this:
public void writeImageToSocket(Image i, Socket s) throws IOException {
OutputStream os = s.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream( os );
oos.writeObject( i );
oos.close();
}
An alternative might be to build upon existing technologies and use a webserver and browser. Your server might then just return the URL of the image in question.
Cheers,
John
Very good point! The problem ( I told you it was untested!) with the above is that the Image class isn't actually serialisable.
The solution depends on how you're storing your images and how you're managing them within your server. It also depends on your proposed client. For example, if your client was a browser and your images files on a disk, then you might use:
Socket s;
// Write out header
PrintWriter pw = new PrintWriter(s.getOutputStream());
pw.println("Content-Type: image/gif");
// And send out data.
byte[] buffer = new byte[2048];
int count;
while((count = fis.read(buffer))>0) {
s.getOutputStream().write(buffer, 0, count);
}
s.getOutputStream().close();
}
although you'd probably be better off using a proper webserver.If you're using a proprietry client, then you can either use Swing's JEditorPane to act like browser and display the image as the server above presents it.
Otherwise, your client could use the incoming byte stream to create a BufferedImage instance.
How are you storing your images and how are you presenting them?
the images are in my hard disk...what i try to do is that when
my client request an image i.e.(photo.jpg) my server sends photo.jpg to the client and then the client shows that image
first, when the connection is made, my server sends the list of the images to my client then this client shows the list in a JList...(server sends a String vector [ ] )
now when the list is loaded the client can choose any of the filenames connect again with the server requesting that specific image file and send it to the client...so this can show it
Well then I guess one thing to do is to adapt that piece of server code I posted above. Your client might then use:
Image image = Toolkit.getDefaultToolkit().getImage( url );
where "url" is the URL of your server with the appropriate filename.
Hope this helps, I'm off for lunch now.
Cheers,
John