Help sending Image from server

Hello,

I am implementing a client/server card game that runs by sending instructions as strings back and forth.

while(true)

{

try

{

String s = input.readUTF();

processMessage(s);

}

catch(IOException io)

{

io.printStackTrace();

}

}

where:

output =new DataOutputStream(connection.getOutputStream());

input =new DataInputStream(connection.getInputStream());

This is all ok but now I would like to be able to send an image (eg. of a player) from the server to a client and I am unable to do this.

I realize (i think) that I need to create a byte[] of the image file on the server-side (below is what I tried)

String where ="cards/" + fname;

if (where.indexOf("..") > -1)

thrownew SecurityException("No access to parent dirs");

System.out.println("looking for " + where);

File f =new File(where);

DataInputStream din =new DataInputStream(new FileInputStream(f));

int len = (int) f.length();

byte[] buf =newbyte[len];

din.readFully(buf);

output.write(buf);

and on the client side something like

byte[] buffer =newbyte[1024];

input.readFully(buffer);

ImageIcon =new ImageIcon(buffer);

Could someone please point me in the right direction.

Thanks and regards

[2324 byte] By [Tizzle4Shizzlea] at [2007-10-3 10:45:07]
# 1
Make the server send the length first (as an int). Let the client read that int, and initialize a buffer of correct length.Kaj
kajbja at 2007-7-15 6:09:24 > top of Java-index,Archived Forums,Socket Programming...