Image streaming problem

Hi! I'm quite new to java (3 months) and i'm doing a project for school... It's a game, quite similar (if not identical) to Pictionary.

It's almost done.. EXCEPT for the image streaming part, which is causing me real problems (and my programming teachers couldn't help me, for that matter) ...

first of all, the server doesnt send the image it receives back to the users, i get this exception :

Exception in thread "Thread-0" java.lang.ClassCastException: sun.awt.image.ToolkitImage

second of all... the fact that the image streaming won't work somehow influences the chat as well (which i don't understand why, i'm using different sockets for the two streams text/image so... one shouldn't affect the other, right?)

this are the two methods i used:

public void writeObject(Socket sImage, Image im) throws java.io.IOException

{

ObjectOutputStream stream=new ObjectOutputStream(sImage.getOutputStream());

PixelGrabber grabber = new PixelGrabber(im, 0,0, -1, -1, true);

try

{

grabber.grabPixels();// methode booleene - on demande les pixels

}

catch (InterruptedException e)

{

e.printStackTrace();

}

Object pix = grabber.getPixels();// on recupere les pixels

Dimension dim = new Dimension(((BufferedImage)im).getWidth(),((BufferedImage)im).getHeight());

// on recupere les dimensions de l'image

stream.writeObject(dim);// on envoye les dimensions

stream.writeObject(pix);// on envoye les pixels

}

public void readObject(ObjectInputStream stream) throws java.io.IOException

{

try

{System.out.println("on essaye de lire");

Dimension dim = (Dimension)stream.readObject();//on lit la dimension

System.out.println(("dimension"+dim.toString()));

Object im = stream.readObject();//on recupere les pixels de l'image

int [] pix = (int [])im;//on "cast" les pixels de l'image

Canvas can = new Canvas();

image = can.createImage(new MemoryImageSource(dim.width, dim.height, pix, 0, dim.width)); // creer image

}

catch (ClassNotFoundException e)

{

e.printStackTrace();

}

}

Ignore the french comments, they're for my teacher :)

So, please help!! I have only two days left to finish it and it's been more than a week that i've been trying to do this thing but i guess i still have a lot to learn!

thanks!!

[2478 byte] By [D@naa] at [2007-11-27 6:46:19]
# 1

> first of all, the server doesnt send the image it receives back to the

> users, i get this exception :

> Exception in thread "Thread-0" java.lang.ClassCastException:

> sun.awt.image.ToolkitImage

// try add this to check what pix value

System.out.println("debug : pix = "pix);

stream.writeObject(pix);

i think pix was null so writeObject(null) don't send anything to client socket.

> second of all... the fact that the image streaming won't work somehow

> influences the chat as well (which i don't understand why, i'm using

> different sockets for the two streams text/image so... one shouldn't

> affect the other, right?)

how you establish that two different socket? please post a little code. (Edit: please use code tags whenever you post code)

Message was edited by:

j_shadinata

j_shadinataa at 2007-7-12 18:18:49 > top of Java-index,Java Essentials,Java Programming...
# 2
You seem to be casting an object reference to a class which it is not. The stacktrace tells you the line number, look at it and see what you're doing wrong.Also, why are you catching ClassDefNotFound... that doesn't look right at all.Message was edited by: -Kayaman-
-Kayaman-a at 2007-7-12 18:18:49 > top of Java-index,Java Essentials,Java Programming...
# 3

10x a lot for answering.

so...

i added the line to check if pix was null ...

public void writeObject(Socket sImage, Image im) throws java.io.IOException

{

ObjectOutputStream stream=new ObjectOutputStream(sImage.getOutputStream());

PixelGrabber grabber = new PixelGrabber(im, 0,0, -1, -1, true);

try

{

grabber.grabPixels();// methode booleene - on demande les pixels

}

catch (InterruptedException e)

{

e.printStackTrace();

}

Object pix = grabber.getPixels();// on recupere les pixels

System.out.println("debug1 : pix = "+pix);

Dimension dim = new Dimension(((BufferedImage)im).getWidth(),((BufferedImage)im).getHeight());

// on recupere les dimensions de l'image

stream.writeObject(dim);// on envoye les dimensions

System.out.println("debug2 : pix = "+pix);

stream.writeObject(pix);// on envoye les pixels

}

I added it in two places because the first time i put it just before stream.writeObject(pix)

but in fact, it never reaches that line. It blocks in between, when i try to cast im to BufferedImage.... Debug1 shows that pix isn't null.

I get the dimension right: dimensionjava.awt.Dimension[width=492,height=430]

and i also get debug1 : pix = [I@11ddcde

As for the two sockets, i have this on the server side:

s=new Socket();

socketImage=new Socket();

ss=new ServerSocket(60001);

ssImage=new ServerSocket(60002);

then i create the streams

BufferedReader din = new BufferedReader(new InputStreamReader( socket.getInputStream() ));

BufferedWriter dout = new BufferedWriter(new OutputStreamWriter( socket.getOutputStream() ));

ObjectOutputStream imageOut=new ObjectOutputStream(socketImage.getOutputStream() );

ObjectInputStream imageIn=new ObjectInputStream(socketImage.getInputStream() );;

and then, i use the two methods to get the image, and than pass it on to all the clients (that are kept in an array list)

(this part in in a class named ServerThread and the two methodes and the array list are in a class Server)

try {

while (j.getStatut().equals("dessinateur"))

{

server.readObject(imageIn);//on essaie de lire les pixels

System.out.println("Recu!!");

if(server.image==null) System.out.println("crap");

System.out.println("nb utilisateurs"+server.monTableau.size());

for (int i=0; i<server.monTableau.size();i++) //on envoye les pixels a toute le monde

{Socket socketIm=server.monTableau.get(i).getSocketImage();

server.writeObject(socketIm,server.image);

}

sleep(100);

}

}

catch (InterruptedException e)

{ e.printStackTrace();

}

on the client side, i have

socket = new Socket( host, 60001 ); //socket texte

socketImage=new Socket(host, 60002); //socket image

System.out.println( "on est connecte a "+socket );

ta.setText( "Introduisez le login:" );

//creation de flux entree/sortie

din = new BufferedReader(new InputStreamReader( socket.getInputStream() ));

dout = new BufferedWriter(new OutputStreamWriter( socket.getOutputStream() ));

imageOut=new ObjectOutputStream(socketImage.getOutputStream() );

imageIn=new ObjectInputStream(socketImage.getInputStream() );

and the, to send the image:

canvas=dessinateur.frame.monCanvas1;

try{ writeObject(imageOut,canvas.createImage(canvas.getSize().width,canvas.getSize().height));}

catch( IOException ie ) {System.out.println("ma-sa!");}//on envoye l'image au serveur

imageOut.flush();

So if you have any ideas,... i'll be more than happy to hear them. and thanks again for looking into the problem!>

D@naa at 2007-7-12 18:18:49 > top of Java-index,Java Essentials,Java Programming...
# 4

the cast problem occurs here:

Dimension dim = new Dimension(((BufferedImage)im).getWidth(),((BufferedImage)im).getHeight());

im is declared as Image so i can't understand why i can't cast it to BufferedImage.

as for the ClassNotFound exception, i work with JDev so it surrounds your code automatically with try/catch.

thanks!

D@naa at 2007-7-12 18:18:49 > top of Java-index,Java Essentials,Java Programming...
# 5
Why not do a debug just before you cast ... if (im instance of BufferedImage) System.out.println("Is Good!"); ... Then you won't be guessing.
abillconsla at 2007-7-12 18:18:49 > top of Java-index,Java Essentials,Java Programming...