Applet <==> Servlet Synchronization?

M

Mustafa Aydin

Hello NG,

Last week I asked you how to make a communication
between an applet and a servlet.

Thanks for all your help. I finally decided to use the variant,
by which the requests and responds are transferred via
OutputStream..

It worked fine from the beginning... locally under Windows 2000.

Because as I let run the same constellation on a XP, I often got
-- either on the server or on the applet site -- a

java.io.StreamCorruptedException: invalidStreamHeader


Because sometimes the program transferred the data correct,
I assumed that there is a kind of synchronization problem.

For test purposes I "hacked" a Thread.sleep(1000) AFTER sending
the request and BEFORE receiving the response.. From then on
all worked fine.

However, as soon as i tried to call the servlet at my provider,
I got the same exception with every call. Increasing the sleep-period
has no effect.

Also trying to wait inside the applet by using "available()" doesn't
work, because also locally (where the data gets transmitted) abailable()
always returns 0.

At the end of this posting I attached the important lines

Do I basically make something false?

Again: With a local server, everything works fine.

Thanks in Advance

Mustafa Aydin

Applet/Application Seite
========================

protected Object send(String send) {return send(send, null);}
protected Object send(String send, Object info) {
Object result = "empty";
try {
URLConnection con = servlet.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestProperty("Content-Type",
"application/x-java-serialized-object");
OutputStream outstream = con.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstream);
oos.writeObject(send);
oos.writeObject(info);
oos.flush();
oos.close();

try {Thread.sleep(100);} catch (InterruptedException e) {}

InputStream instr = con.getInputStream();
ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
result = inputFromServlet.readObject();
inputFromServlet.close();
instr.close();
} catch (Exception ex) {
result = ex.toString();
}
return result;
}

Server-Seite
===========
public void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
try {
response.setContentType("application/x-java-serialized-object");
InputStream in = request.getInputStream();
ObjectInputStream inputFromApplet = new ObjectInputStream(in);

String order = (String) inputFromApplet.readObject();
Object info = inputFromApplet.readObject();
Object answer = execute(order, info);

OutputStream outstr = response.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstr);
oos.writeObject(answer);
oos.flush();
oos.close();
} catch (Exception e) {
System.err.println(e.toString());
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top