Note that there are some explanatory texts on larger screens.

plurals
  1. POObjectInputStream and getInputStream
    primarykey
    data
    text
    <p>I'm working on a school project and the following codes are an example provided for building the project (should work without any problem but not...). There was no compiling error but when I use telnet to test it the following message shows:</p> <pre><code>java.io.StreamCorruptedException: invalid stream header: 56543130 at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:801) at java.io.ObjectInputStream.&lt;init&gt;(ObjectInputStream.java:298) at ThreadedDataObjectHandler.run(ThreadedDataObjectServer.java:41) </code></pre> <p>Line 41 is </p> <pre><code>ObjectInputStream in =new ObjectInputStream(incoming.getInputStream()); </code></pre> <p>Here are my codes:</p> <pre><code>import java.io.*; import java.net.*; import java.util.*; public class ThreadedDataObjectServer { public static void main(String[] args ) { try { ServerSocket s = new ServerSocket(3000); for (;;) { Socket incoming = s.accept( ); new ThreadedDataObjectHandler(incoming).start(); } } catch (Exception e) { e.printStackTrace(); } } } class ThreadedDataObjectHandler extends Thread { DataObject myObject = null; private Socket incoming; private String greeting="Hello!"; public ThreadedDataObjectHandler(Socket incoming) { this.incoming = incoming; } public void run() { try { ObjectInputStream in =new ObjectInputStream(incoming.getInputStream()); ObjectOutputStream out =new ObjectOutputStream(incoming.getOutputStream()); myObject = (DataObject)in.readObject(); System.out.println("Message read: " + myObject.getMessage()); myObject.setMessage("Got it!"); System.out.println("Message written: " + myObject.getMessage()); out.writeObject(myObject); in.close(); out.close(); incoming.close(); } catch (Exception e) { e.printStackTrace(); } } } class DataObject implements Serializable{ protected String message; public DataObject(){ message=""; } public void setMessage(String m){ message=m; } public String getMessage(){ return message; } } </code></pre> <p>What I tried was to switch the order of statements <code>ObjectInputStream in=...</code> and <code>ObjectOutputStream out=...</code> but no luck. Please help...thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload