title: socket keeps closing
body: <p><strong>EDIT:</strong> on the server-side, if I remove the code from the run method in ServerWriter class and put it into the send order method, removing the code for threading, it starts working properly again, socket doesn't close. but I need it to be multithreaded, I don't understand why the sockets close with threading. Code is shown below</p>
  
  <pre><code>    public void sendOrder(Order o){
  //      ServerWriter sw=new ServerWriter(serversocket,o);
  //      Thread t=new Thread(sw);
  //      t.start();
          ObjectOutputStream out=null;
          if(serversocket.isClosed()){
              System.out.println("closed");
          }
          else{
              System.out.println("notclosed");
          }
          try {
              out=new ObjectOutputStream(serversocket.getOutputStream());
              out.writeObject(o);
              out.reset();
  
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
      }
  </code></pre>
  
  <p><strong>edit:</strong> I think the socket is closing on the server-side, not the client-side, deducing this from the output of server where it prints closed on checking <code>isclosed()</code>, whereas on the client-side it says Not Closed</p>
  
  <p>I have a socket program. I have two methods in the client 1 for opening the socket and another for reading the data. every time I try to run this code I keep getting <code>eofexception</code>, which mean the socket is closed.</p>
  
  <p>I had this error the last time I was working on this code a few days back so I just removed all the code from reading and pasted it in the open method and it started working (I didn't really understand why).</p>
  
  <p>Today I reverted back and just moved the code back to the read method and everything started working perfectly, the connection wasn't closing by itself. then I made the read part a thread and still everything was working fine, I was working on the write parts of the code and now when I run it I get the same <code>eofexception</code> again, I haven't changed anything in the read code, so I have no idea why this is happening. on the server-side I have done a similar thing where I accept a socket in a method and send data through a thread. On the server-side I get a SocketException.</p>
  
  <p>I could really use some help in shedding some light on what I am doing wrong. </p>
  
  <p>Following is the client-side code</p>
  
  <pre><code>public void open(){
      try
      {
          s=new Socket(InetAddress.getLocalHost(),12345);
  
          s.setKeepAlive(true);
  
      }
  
      catch(UnknownHostException u)
      {
          System.err.println("I don't know host");
  
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } 
  
  
  }
  
  
  
  public void read(){
      ClientReader cr=new ClientReader(s);
      Thread t=new Thread(cr);
      t.start();
  }
  
  
  class ClientReader implements Runnable{
      Socket s;
      ClientReader(Socket s){
          this.s=s;
      }
      public void run(){
          InputStream is;
          ObjectInputStream ois=null;
          try {
              is = s.getInputStream();
                      if(!(s==null)){
                  System.out.println("not null");
              }
                      **//outputs NotClosed here**
          if(s.isClosed()){
              System.out.println("CLOSED");
          }
                      else system.out.println("Not Closed")
  
                //Throws EOFException here
           ois= new ObjectInputStream(is); 
          } catch (IOException e2) {
              // TODO Auto-generated catch block
              e2.printStackTrace();
          }
          Order order;
           while(true){
               try{
               order=(Order)ois.readObject();
               Client_Socket.ll.add(order);
              System.out.println(order);
              System.out.println(order.getTotal()); 
  
               }
               catch(EOFException e){
  
                   try{
                   ois.close();
                   s.close();
                   break;
  
               } catch (IOException e1) {
                      // TODO Auto-generated catch block
                      e1.printStackTrace();
                  }
               } catch (ClassNotFoundException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
           }
      }
  }
  </code></pre>
  
  <p>The <strong>main</strong> method for calling the client </p>
  
  <pre><code>public static void main(String[] args) {
      // TODO Auto-generated method stub
      Client_Socket cs=new Client_Socket();
      cs.open();
      cs.read();
  
  }
  </code></pre>
  
  <p><strong>client-side exception</strong></p>
  
  <pre><code>not null
  Not Closed
  
  java.io.EOFException
      at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
      at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
      at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
      at java.io.ObjectInputStream.&lt;init&gt;(Unknown Source)
      at ClientReader.run(Client_Socket.java:82)
      at java.lang.Thread.run(Unknown Source)
  Exception in thread "Thread-0" java.lang.NullPointerException
      at ClientReader.run(Client_Socket.java:91)
      at java.lang.Thread.run(Unknown Source)
  </code></pre>
  
  <p><strong>exception on the server-side</strong></p>
  
  <pre><code>Connection from Socket[addr=/192.168.0.108,port=50380,localport=12345]
  closed
  
  java.net.SocketException: Socket is closed
      at java.net.Socket.getOutputStream(Unknown Source)
      at ServerWriter.run(Server_Socket.java:123)
      at java.lang.Thread.run(Unknown Source)
  </code></pre>
  
  <p><strong>server-side code snippet</strong></p>
  
  <pre><code>public void openSocket(){
      try
      {
          service=new ServerSocket(12345);
  
          serversocket=service.accept();
          serversocket.setKeepAlive(true);
          System.out.println("Connection from "+serversocket);
  
  
  
      } catch (IOException e) {
          // TODO Auto-generated catch block
          System.out.println("output stream error");
          System.out.println(e);
      }
  
  }
  
  
  public void sendOrder(Order o){
      ServerWriter sw=new ServerWriter(serversocket,o);
      Thread t=new Thread(sw);
      t.start();
  
  }
  
  
  class ServerWriter implements Runnable{
      Socket serversocket;
      Order o;
      ServerWriter(Socket s,Order o){
          this.serversocket=s;
          this.o=o;
      }
  
      public void run() {
          ObjectOutputStream out=null;
          if(serversocket.isClosed()){
              System.out.println("closed");
          }
          else{
              System.out.println("notclosed");
          }
          try {
              //SocketException here
              out=new ObjectOutputStream(serversocket.getOutputStream());
              out.writeObject(o);
              out.reset();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>method snippet for calling the server</p>
  
  <pre><code>public static void main(String[] args) {
      // TODO Auto-generated method stub
      Order o=new Order();
      Server_Socket ss=new Server_Socket();
      ss.openSocket();
      ss.sendOrder(o);
      ss.close();
  }
  </code></pre>