bored_shiva
asked on
ObjectOutputStream and ObjectInputStream errors
Hi,
Below are two snippets of code. The first one (starting with while(true)) is suppose to wait for a connection on a serversocket, get an ObjectOutputStream and ObjectInputStream for the socket it returns, transmit an object of type "message", and get back the same type of object.
The second snippet is suppose to connect to the program running the first snippet, get a message, and transmit back a message. These two parts are suppose to work as mirror images of each other, IE the output stream of one is the input stream of the other.
The flow is as follows:
1 First snippet runs, waits for accept.
2 Second snippet runs, connects to the port where the first waits
3 First accepts, opens a socket, gets an ObjectOutputStream and sends a message
4 Second gets message from its ObjectInputStream (the other end of the first's output) and transmits back a message on its outputstream
5 First gets message from second on its InputStream which is the other end of the seconds's output stream
Basically, this is a handshake senario, however, when I run both programs, the first snippet gives the following error:
java.io.EOFException
at java.io.ObjectInputStream$ PeekInputS tream.read Fully(Obje ctInputStr eam.java:2 281)
at java.io.ObjectInputStream$ BlockDataI nputStream .readShort (ObjectInp utStream.j ava:2750)
at java.io.ObjectInputStream. readStream Header(Obj ectInputSt ream.java: 780)
at java.io.ObjectInputStream. <init>(Obj ectInputSt ream.java: 280)
at dimes.comm2gui.GUIConnecto rThread.ru n(GUIConne ctorBean.j ava:165)
(line 165 is marked with ---> in the code below)
On the other side of the socket, the second snippet (which is suppose to receive the message after it connects) gives this error:
java.lang.ClassNotFoundExc eption: dimes.comm2gui.common.Mess age
at java.net.URLClassLoader$1. run(URLCla ssLoader.j ava:200)
at java.security.AccessContro ller.doPri vileged(Na tive Method)
at java.net.URLClassLoader.fi ndClass(UR LClassLoad er.java:18 8)
at java.lang.ClassLoader.load Class(Clas sLoader.ja va:307)
at sun.misc.Launcher$AppClass Loader.loa dClass(Lau ncher.java :301)
at java.lang.ClassLoader.load Class(Clas sLoader.ja va:252)
at java.lang.ClassLoader.load ClassInter nal(ClassL oader.java :320)
at java.lang.Class.forName0(N ative Method)
at java.lang.Class.forName(Cl ass.java:2 47)
at java.io.ObjectInputStream. resolveCla ss(ObjectI nputStream .java:604)
at java.io.ObjectInputStream. readNonPro xyDesc(Obj ectInputSt ream.java: 1575)
at java.io.ObjectInputStream. readClassD esc(Object InputStrea m.java:149 6)
at java.io.ObjectInputStream. readOrdina ryObject(O bjectInput Stream.jav a:1732)
at java.io.ObjectInputStream. readObject 0(ObjectIn putStream. java:1329)
at java.io.ObjectInputStream. readObject (ObjectInp utStream.j ava:351)
at dimes.comm2agent.AgentConn ectorBean. initConenc tion(Agent ConnectorB ean.java:8 6)
(Line 86 is marked with a --->)
I assume both of these problems are part of the same basic issue. But I don't know what it is. From what I've been reading the trouble seems to be with the Object Header not being read completely or something of that nature. I attempted to flush the Outputstream after I start it, in the hope that it may transmit the header, but that didn't seem to work.
Any ideas?
Thanks
Below are two snippets of code. The first one (starting with while(true)) is suppose to wait for a connection on a serversocket, get an ObjectOutputStream and ObjectInputStream for the socket it returns, transmit an object of type "message", and get back the same type of object.
The second snippet is suppose to connect to the program running the first snippet, get a message, and transmit back a message. These two parts are suppose to work as mirror images of each other, IE the output stream of one is the input stream of the other.
The flow is as follows:
1 First snippet runs, waits for accept.
2 Second snippet runs, connects to the port where the first waits
3 First accepts, opens a socket, gets an ObjectOutputStream and sends a message
4 Second gets message from its ObjectInputStream (the other end of the first's output) and transmits back a message on its outputstream
5 First gets message from second on its InputStream which is the other end of the seconds's output stream
Basically, this is a handshake senario, however, when I run both programs, the first snippet gives the following error:
java.io.EOFException
at java.io.ObjectInputStream$
at java.io.ObjectInputStream$
at java.io.ObjectInputStream.
at java.io.ObjectInputStream.
at dimes.comm2gui.GUIConnecto
(line 165 is marked with ---> in the code below)
On the other side of the socket, the second snippet (which is suppose to receive the message after it connects) gives this error:
java.lang.ClassNotFoundExc
at java.net.URLClassLoader$1.
at java.security.AccessContro
at java.net.URLClassLoader.fi
at java.lang.ClassLoader.load
at sun.misc.Launcher$AppClass
at java.lang.ClassLoader.load
at java.lang.ClassLoader.load
at java.lang.Class.forName0(N
at java.lang.Class.forName(Cl
at java.io.ObjectInputStream.
at java.io.ObjectInputStream.
at java.io.ObjectInputStream.
at java.io.ObjectInputStream.
at java.io.ObjectInputStream.
at java.io.ObjectInputStream.
at dimes.comm2agent.AgentConn
(Line 86 is marked with a --->)
I assume both of these problems are part of the same basic issue. But I don't know what it is. From what I've been reading the trouble seems to be with the Object Header not being read completely or something of that nature. I attempted to flush the Outputstream after I start it, in the hope that it may transmit the header, but that didn't seem to work.
Any ideas?
Thanks
private ServerSocket comm;
private Socket lComm;
private GUIConnectorBean bean;
private String xml;
private ObjectOutputStream oOStream;
private ObjectInputStream iOStream;
private Message Current;
private GUICommunicator Communicator;
while(true){
try {
lComm=comm.accept(); //Wait for connection from GUI
oOStream = new ObjectOutputStream(lComm.getOutputStream());
oOStream.flush();
Message msg = new Message(MessageTypes.DIMESACK, -1, "DIMES", new Object[]{bean.getAgentVersion()});
oOStream.writeObject(msg);
InputStream iStream = lComm.getInputStream();
-----> iOStream = new ObjectInputStream(iStream);
Current = (Message)iOStream.readObject();
if (MessageTypes.DIMESACK==Current.getType());
else continue;
bean.setObjectOutputStream(oOStream);
Communicator = GUICommunicator.getInstance();
bean.setConnectionTimer();
break;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
--------------------------------------------------------------------------------------------------------------
private Socket commSocket = new Socket();
private InetAddress localHost;
private Logger logger;
private Thread iP;
private ObjectInputStream iOStream;
private ObjectOutputStream oOStream;
commSocket.connect(new InetSocketAddress(localHost, CONNECT_PORT),SOCKET_TIMEOUT);
iOStream = new ObjectInputStream(commSocket.getInputStream());
---> Message msg = (Message) iOStream.readObject();
if (!(MessageTypes.DIMESACK==msg.getType())) return false;
inputString = msg.getAttrib();
oOStream = new ObjectOutputStream(commSocket.getOutputStream());
oOStream.writeObject(new Message(MessageTypes.DIMESACK, -1, "", null)); //Send an empty ACK message to Agent to finish handshake
iP = new Thread (new inputProcessor(iOStream, logger));
iP.start();
return true;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER