title: java.net.SocketException: Socket is closed TCP Connection
body: <p>Doing some debugging, I think the error occurs in this method below. The method basically sends an ID (string) to the server and the server after receiving this ID looks up a record which it sends back to the client. The IDs are displayed in a GUI JList which the user selects so each time the user clicks on a different ID in the JList, this method is activated by the ListSelectionListener event. Once the client receives the record from the server, it displays it in a JTextField. Everything works as expected when user first clicks an ID in JList. But when you click another record in the list, I get the exception: </p>
  
  <blockquote>
    <p>java.net.SocketException: Socket is closed </p>
  </blockquote>
  
  <p>Edit: Now I'm getting:</p>
  
  <blockquote>
    <p>java.net.SocketException: Software caused connection abort: recv failed</p>
  </blockquote>
  
  <p>Client program:</p>
  
  <pre><code>    import java.awt.BorderLayout;
  import java.awt.Dimension;
  import java.io.IOException;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  import java.net.Socket;
  import java.net.UnknownHostException;
  import java.util.ArrayList;
  
  import javax.swing.DefaultListModel;
  import javax.swing.JFrame;
  import javax.swing.JList;
  import javax.swing.JPanel;
  import javax.swing.JScrollPane;
  import javax.swing.JTextField;
  import javax.swing.event.ListSelectionEvent;
  import javax.swing.event.ListSelectionListener;
  
  
  public class ClientGUI extends JFrame implements ListSelectionListener{
  
      //gui components
      JList jlist = null;
      String requestID = null; //Assigned to selected ID in JList. 
      JScrollPane scpane = null;
      JTextField field = null;
      JPanel pane = null;
      DefaultListModel&lt;String&gt; listModel = null;
      ArrayList&lt;String&gt; idList = null;
  
      //client stuff:
      Socket sock1 = null;
      Socket sock2 = null;
      ObjectInputStream in = null;
      ObjectOutputStream out = null;
  
      public ClientGUI() throws ClassNotFoundException{
          //get List of IDs from Server1
          try{
              sock1 = new Socket("FahadAhmed-PC", 8889); 
              in = new ObjectInputStream(sock1.getInputStream());
              idList = new ArrayList&lt;String&gt;(29);
              ArrayList&lt;Customer&gt; custList = null;
              custList = (ArrayList&lt;Customer&gt;) in.readObject();
              for(Customer c : custList){
                  idList.add(c.getID());
              }
  
              in.close();
              sock1.close();
  
              sock2 = new Socket("FahadAhmed-PC", 8888); 
              ArrayList streams = new ArrayList(3); 
              streams.add(out = new ObjectOutputStream(sock2.getOutputStream()));
              streams.add(in = new ObjectInputStream(sock2.getInputStream()));
  
          }catch(UnknownHostException e) {
              System.err.println("Don't know about host: FahadAhmed-PC");
              System.exit(1);
          }catch(IOException e){
              System.err.println(e);
              System.exit(1);
          }
  
          //Setup GUI
          jlist = new JList(idList.toArray());
          jlist.setVisibleRowCount(10);
          scpane = new JScrollPane(jlist);
          jlist.addListSelectionListener(this);
          pane = new JPanel(new BorderLayout());
          pane.setPreferredSize(new Dimension(300, 300));
          field = new JTextField(29);
          field.setEditable(false);
          pane.add(scpane, BorderLayout.PAGE_START);
          pane.add(field, BorderLayout.PAGE_END);
  
          this.setContentPane(pane);
          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          this.pack();
          this.setVisible(true);
      }
  
      public static void main(String args[]) throws ClassNotFoundException{
          ClientGUI gui = new ClientGUI();
      }
  
  
      @Override
      public void valueChanged(ListSelectionEvent arg0) {
          if(!arg0.getValueIsAdjusting())
              try {
                  System.out.println(jlist.getSelectedValue().toString());
                  getRecord(jlist.getSelectedValue().toString());
              } catch (ClassNotFoundException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }       
      }
  
      private void getRecord(String getID) throws ClassNotFoundException {
          try{
  
              //System.out.println(getID + "sent to getRecord method");
              out.writeObject(getID);
  
              String rec = (String) in.readObject();
  
              field.setText(rec);
              //in.close();
              //sock2.shutdownInput();
              out.flush();
              //sock2.shutdownOutput();
              //out.close();
  
          }catch(UnknownHostException e) {
              System.err.println("Don't know about host: FahadAhmed-PC");
              System.exit(1);
          }catch(IOException e){
              System.err.println(e);
              System.exit(1);
          }
      }
  
  
  }
  </code></pre>