Note that there are some explanatory texts on larger screens.

plurals
  1. POWhere and when should I use the close() method to avoid IOException in ObjectInputStream?
    primarykey
    data
    text
    <p>I'm trying to read an object from client program over tcp. As you can see in this line I created objectInput:</p> <pre><code>ObjectInputStream objectInput = new ObjectInputStream(incoming.getInputStream()); </code></pre> <p>And then read my input from the other program. It used to work fine until i made minor changes to clean up the program. Personally Assume I added </p> <pre><code>objectInput.clsoe(); </code></pre> <p>My question is, After reading the object should I close the objectInputStream or Keep remain without close? Should I close it straight away after using it or at the end of if block or at the end of program? What are the effect of the close? By the way I have read the close documentation.</p> <p>Here is the error:</p> <pre><code>java.io.EOFException at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2280) at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2749) at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:779) at java.io.ObjectInputStream.&lt;init&gt;(ObjectInputStream.java:279) at Server.ClientWorker.run(MyCollectionServer.java:116) at java.lang.Thread.run(Thread.java:680) </code></pre> <p>Here is my code:</p> <pre><code> public static void main(String[] args) { ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(port); } catch (IOException e) { e.printStackTrace(); } while(true) { ClientWorker w; try { w = new ClientWorker(serverSocket.accept()); Thread t = new Thread(w); t.start(); } catch(IOException e) { e.printStackTrace(); break; } } } } class ClientWorker implements Runnable { ..... private Socket incoming; public ClientWorker(Socket incoming) { myList = new ArrayList&lt;PureAlbum&gt;(); loadList(); this.incoming = incoming; } ..... public synchronized void run() { else if(request.compareTo("AddAlbum")==0) { try { ObjectInputStream objectInput = new ObjectInputStream(incoming.getInputStream()); //This is the line mentioned in the error PureAlbum object = (PureAlbum) objectInput.readObject(); if(object instanceof CDAlbum) { CDAlbum a = (CDAlbum) object; myList.add(a); System.out.println("Artist = " + a.getArtist()); } else if(object instanceof Client.au.edu.uow.Collection.DVDAlbum) { myList.add((DVDAlbum) object); } else { System.err.println("Warning : The object to add to database is unknown! "+ object.getClass() + "*"); System.exit(0); } } catch (UnknownHostException e) { System.err.println("Can not read the host name"); e.printStackTrace(); } catch (IOException e) { System.err.println("Can not read the FILE name"); //This exception has been called e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } </code></pre>
    singulars
    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.
    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