sending an image using sockets

can anybody give an idea about how to send an image using sockets.....what i want to do is a client that connects to a server this server has the images...so the client request any image that the server has and display it int the client's frame
[259 byte] By [pompeighuIIa] at [2007-10-2 21:11:19]
# 1

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

john_greenhowa at 2007-7-13 23:57:30 > top of Java-index,Java Essentials,Java Programming...
# 2
so......the only thing i have to do is:public class myclass implements Serializable.....and the pass the image as when i pass string objects?
pompeighuIIa at 2007-7-13 23:57:30 > top of Java-index,Java Essentials,Java Programming...
# 3

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?

john_greenhowa at 2007-7-13 23:57:30 > top of Java-index,Java Essentials,Java Programming...
# 4

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

pompeighuIIa at 2007-7-13 23:57:30 > top of Java-index,Java Essentials,Java Programming...
# 5

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

john_greenhowa at 2007-7-13 23:57:30 > top of Java-index,Java Essentials,Java Programming...