title: Java - closing socket gives different results
body: <p>I'm getting different results while trying to read from a socket while the other socket is closed.</p>
  
  <p>I have two sockets A and B.</p>
  
  <p>1)B sent some data to A --> A has read the data --> A closes -->  When B tries to read some data from A, it is getting -1(or EOF).</p>
  
  <p>2)B sent some data to A --> A closes even before reading the data --> Now B tries to read from A, an exception is thrown(java.net.SocketException "Software caused connection abort.")</p>
  
  <p>please excuse me, if you can't understand my question. Please see the code </p>
  
  <p>Server.java</p>
  
  <pre><code>import java.io.*;
  import java.net.*;
  
  
  class SocketCloser extends Thread
  {
      private Socket c;
      public SocketCloser(Socket c) {
          this.c = c;
      }
  
      public void run()  {
  
          try{
              this.c.close();
          } catch (Exception e) {}
      }
  }
  
  
  public class Server
  {
      public static void main(String argv[]) throws Exception {
          ServerSocket listen = new ServerSocket(6789);
  
          Socket socket = listen.accept();
          SocketCloser sc = new SocketCloser(socket);
          InputStream is = socket.getInputStream();
          // uncomment below line to get "Software caused connection abort" on client
          //sc.start();
          try {
              Thread.sleep(1000);
              int i = is.read();
              System.out.println("read returned: " + i);
              socket.close();
          } catch (Exception e) {
              System.out.println(e.toString() + " thrown");
          }
      }
  }
  </code></pre>
  
  <p>Client.java</p>
  
  <pre><code>import java.io.*;
  import java.net.*;
  
  public class Client
  {
      public static void main(String argv[])
      {
          Socket cSocket;
          try {
              cSocket = new Socket("localhost", 6789);
              InputStream is = cSocket.getInputStream();
              OutputStream os = cSocket.getOutputStream();
              Thread.sleep(1000);
              os.write(200);
              Thread.sleep(1000);
              int i = is.read();
              System.out.println("read returned: " + i);
              cSocket.close();
          } catch (Exception e) {
              System.out.println(e.toString() + " thrown");
          }
      }
  }
  </code></pre>
  
  <p>Can someone please help me figure out why there is an exception in one case and -1 in another. Interestingly on linux both the cases resulted in -1.</p>