This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
same
Posted by panu_phot on November 07, 2001 at 1:58 AM
> HI, > I have no idea of solution. > I try to make an application-servlet communication but when I > get the inputStream with new ObjectInputStream(myServletInputStream), I have the StreamCorruptedException > If you have a solution? > thanks. > > I am writing a simple java server, that listens on port 95, spawns a thread for every socket that connects, and then attempts to read a serialized object from the socket, then return it to the socket. I have written a test client program, and here is the code for both the client and the server: > > From the client: > > *************************************** > > URL myConnector = new URL("http://"+address+":95"); > > URLConnection myConnection = myConnector.openConnection(); > > myConnection.setDoOutput(true); > > myConnection.setDoInput(true); > > System.out.println("connection open..."); > > //send an object > > DummyObject someObject = new DummyObject(System.currentTimeMillis()); > > OutputStream os = myConnection.getOutputStream(); > > //os.setRequestProperty("Content-Type","application/octet-stream"); > > ObjectOutputStream toServer = new ObjectOutputStream(os); > > System.out.println("Sending DummyObject: "+DummyObject.currTime()); > > toServer.writeObject(someObject); > > toServer.flush(); > > toServer.close(); > > System.out.println("object written to the port"); > > //wait for a response > > ******************************************* > > The server: > > ******************************************* > > //read the object in. > > System.out.println("creating an input stream"); > > InputStream in = mySocket.getInputStream(); > > System.out.println("creating an ObjectInputStream"); > > (dies on this line...)ObjectInputStream fromClient = new ObjectInputStream(in); > > System.out.println("about to read the object"); > > DummyObject someObject = (DummyObject)fromClient.readObject(); > > System.out.println("Received DummyObject: "+DummyObject.currTime()); > > *********************************************** > > And here is the stack trace.... > > ******************************************** > > java.io.StreamCorruptedException: Caught EOFException while reading the stream header > > at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:845) > > at java.io.ObjectInputStream.(ObjectInputStream.java:168) > > at WorkerBee.run(SimpleServer.java:112) > > at java.lang.Thread.run(Thread.java:496) > > Caught EOFException while reading the stream header > > ************************************************> > So it is complaining that it gets an EOF exception when trying to read the header of the socket connection's InputStream... anyone have any ideas? > > Here is the code for my DummyObject class: > > ************************************************* > > class DummyObject implements Serializable > > { > > protected static long currTime; > > public DummyObject(long newTimeLong) > > { > > currTime = newTimeLong; > > } > > //set and accessor methods > > public static long currTime() > > { > > return currTime; > > } > > public void setCurrTime(long newTimeLong) > > { > > currTime = newTimeLong; > > } > > private void writeObject(java.io.ObjectOutputStream out) throws IOException > > { > > //default write thingie... > > System.out.println("writing from the server... "); > > out.defaultWriteObject(); > > } > > private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException > > { > > //default read thingie... > > System.out.println("reading to the server... "); > > in.defaultReadObject(); > > } > > } > > -Kimball Larsen > > quangdog@hotmail.com
Replies:
|