Posted By:
Stuart_Stephen
Posted On:
Wednesday, May 15, 2002 08:25 AM
Hi all, I have a problem, I can recieve messages from the server that I am tunnelling to and send them to my applet fine. However it is proving difficult to do things the other way round, when I want to send a message from the applet to the internal server I am having "java.io.EOFException: code expected" exceptions appear. The connection from the Applet is just a simple URLConnection. This is not closed unless the applet closes or the user quits. I have some tomcat logs to show you what is going wrong. I hope someone can be of help here. Thanks, Stuart Stephen start of servlet
More>>
Hi all,
I have a problem, I can recieve messages from the server that I am tunnelling to and
send them to my applet fine. However it is proving difficult to do things
the other way round, when I want to send a message from the applet to the
internal server I am having "java.io.EOFException: code expected" exceptions
appear.
The connection from the Applet is just a simple URLConnection. This is not
closed unless the applet closes or the user quits.
I have some tomcat logs to show you what is going wrong. I hope someone can
be of help here.
Thanks,
Stuart Stephen
start of servlet
ToApplet()
ToServer()
...Reading for Object Streams
java.io.EOFException: Expecting code
at java.io.ObjectInputStream.peekCode(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at TestTunnel$ToApplet.run(TestTunnel.java:94)
From ChatServer: "some random string object blah blah blah"
...Reading for Object Streams
java.io.EOFException: Expecting code
at java.io.ObjectInputStream.peekCode(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at TestTunnel$ToApplet.run(TestTunnel.java:94)
java.io.EOFException: Expecting code
at java.io.ObjectInputStream.peekCode(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at TestTunnel$ToApplet.run(TestTunnel.java:94)
etc...
My code is as follows.
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletException.*;
import javax.servlet.UnavailableException.*;
import java.lang.Exception.*;
import java.io.*;
import java.util.*;
import java.net.*;
public class TestTunnel extends HttpServlet {
// applet connections
ObjectOutputStream objOut = null;
ObjectInputStream objIn = null;
// chatserver connections
Socket socket = null;
ObjectOutputStream sockObjOut = null;
ObjectInputStream sockObjIn = null;
ToApplet app = null;
ToServer ser = null;
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
try {
// get data stream from applet
System.err.println("start of servlet");
objOut = new ObjectOutputStream(res.getOutputStream());
objIn = new ObjectInputStream(req.getInputStream());
objOut.useProtocolVersion(objOut.PROTOCOL_VERSION_1);
// create socket to chat server and the streams
socket = new Socket("127.0.0.1", 3000);
sockObjOut = new ObjectOutputStream(socket.getOutputStream());
sockObjIn = new ObjectInputStream(socket.getInputStream());
sockObjOut.useProtocolVersion(sockObjOut.PROTOCOL_VERSION_1);
app = new ToApplet(objIn, sockObjOut);
ser = new ToServer(sockObjIn, objOut);
while(ser.connIsAlive() || app.connIsAlive()) {
try {
Thread.sleep(500);
}
catch(Exception ex) {
ex.printStackTrace();
}
}
System.err.println("end of servlet");
}
catch(Exception e) {
e.printStackTrace();
}
}
public void destroy() {
try {
objIn.close();
objOut.close();
sockObjIn.close();
sockObjOut.close();
socket.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
private class ToApplet extends Thread {
ObjectOutputStream objOut = null;
ObjectInputStream sockObjIn = null;
boolean isAlive = true;
public ToApplet(ObjectInputStream sockObjIn, ObjectOutputStream objOut)
{
try {
System.err.println("ToApplet()");
this.objOut = objOut;
this.sockObjIn = sockObjIn;
this.start();
}
catch(Exception e) {
e.printStackTrace();
}
}
public void run() {
try {
Object obj = new Object();
while(obj!=null) {
try {
System.err.println("...Reading for Object Streams");
obj = sockObjIn.readObject();
System.err.println("...Read Object Stream");
write(obj);
}
catch(EOFException eof) {
eof.printStackTrace();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
}
catch(Exception e) {
e.printStackTrace();
}
}
public boolean connIsAlive() {
return isAlive;
}
public void write(Object msg) {
try {
System.err.println("ChatApplet: "+msg.toString());
objOut.writeObject(msg);
objOut.flush();
objOut.reset();
}
catch(Exception e) {
e.printStackTrace();
isAlive = false;
}
}
}
private class ToServer extends Thread {
ObjectInputStream objIn = null;
ObjectOutputStream sockObjOut = null;
boolean isAlive = true;
public ToServer(ObjectInputStream objIn, ObjectOutputStream sockObjOut)
{
try {
System.err.println("ToServer()");
this.objIn = objIn;
this.sockObjOut = sockObjOut;
this.start();
}
catch(Exception e) {
e.printStackTrace();
}
}
public void run() {
try {
Object obj = null;
while((obj=objIn.readObject())!=null) {
write(obj);
}
}
catch(Exception e) {
e.printStackTrace();
isAlive = false;
}
}
public boolean connIsAlive() {
return isAlive;
}
public void write(Object msg) {
try {
System.err.println("ChatServer: "+msg.toString());
sockObjOut.writeObject(msg);
sockObjOut.flush();
sockObjOut.reset();
}
catch(Exception e) {
e.printStackTrace();
}
}
}
}
<<Less