Click to See Complete Forum and Search --> : How to Send byte Array from Applet to SERVER?


dhaubaji
November 4th, 2002, 09:47 PM
Hi!

I have an applet running that allows the user to
draw/create their own image. I need to somehow
capture that and send it to the server.

For this I have tried the following :

Inside applet :

BufferedImage bi = (BufferedImage) createImage(w, hh);
Graphics2D big = bi.createGraphics();

big.drawLine(10,10,20,20);
.....................
.....................
bla bla..

// encodes bi as a JPEG data stream
ByteArrayOutputStream out = new ByteArrayOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
param.setQuality(1.0f, false);
encoder.setJPEGEncodeParam(param);
encoder.encode(bi);

//convert the byteArray to base64
b64.encode(out.toByteArray());


//send the converted byteArray to SERVER and
//do decode process there
...............??

What is the simplest way to send the byte Array to SERVER?

Please Help!

Goodz13
November 4th, 2002, 10:10 PM
Your going to need a ServerSocket In your server and a socket in your applet.

Here's a few good tutorials

http://java.sun.com/docs/books/tutorial/networking/sockets/
http://www.javaworld.com/javaworld/jw-12-1996/jw-12-sockets.html
http://www.ccs.neu.edu/home/kenb/1335/socket_tut.html

dhaubaji
November 5th, 2002, 05:42 AM
Would you be specific about it?

Isnt there any other way except the use of SOCKETS?

Goodz13
November 6th, 2002, 10:15 AM
There isn't anyother way that I know of.

Since there are security issues with Applets, you're going to want to sign your JAR and the user will have to trust it.
Start with this,
http://java.sun.com/docs/books/tutorial/security1.2/toolsign/step2.html
If you go through the steps, it will give you more detail on how to do it than I can.

the following code is untested, so I'm not 100% sure if it works. I just did it up quick rough example

// code for server
ServerSocket server;
Socket connection;

ObjectOutputStream output;
ObjectInputStream input;

String message = "";
try {
server = new ServerSocket(4444);
while ( true ) {
connection = server.accept();

output = new ObjectOutputStream(connection.getOutputStream() );
output.flush();
input = new ObjectInputStream(connection.getInputStream() );
output.writeObject("CON_OK:");
output.flush();
while ((message = (String) input.readObject()) != null) {
if (message.equals("Bye."))
break;
System.out.println(message);
output.writeObject("Got Your Message");
output.flush();
}
output.close();
input.close();
connection.close();
}
}
catch ( EOFException eof ) {
System.out.println( "Client terminated connection" );
}
catch ( IOException io ) {
io.printStackTrace();
}
catch ( Exception io ) {
e.printStackTrace();
}
finally {
connection = null;
output = null;
input = null;
}

// code for Client
Socket connection;

ObjectOutputStream output;
ObjectInputStream input;

String message = "";
try {
// 127.0.0.1 is a generic IP to your own computer.
connection = new Socket("127.0.0.1", 4444);

output = new ObjectOutputStream(connection.getOutputStream() );
input = new ObjectInputStream(connection.getInputStream() );

if (message = (String) input.readObject()) != null) {
System.out.println(message);
output.writeObject("Got Your Message");
output.flush();
if (message = (String) input.readObject()) != null)
output.writeObject("Bye.");

}
output.close();
input.close();
connection.close();
}
catch ( EOFException eof ) {
System.out.println( "Server terminated connection" );
}
catch ( IOException io ) {
io.printStackTrace();
}
catch ( Exception io ) {
e.printStackTrace();
}
finally {
connection = null;
output = null;
input = null;
}

tangjun
April 22nd, 2003, 11:26 AM
what do you mean sign the jar file? after creating the jar file containing the class, what did you do to sign it?

Goodz13
April 22nd, 2003, 12:51 PM
http://java.sun.com/docs/books/tutorial/jar/sign/index.html