title: Client-server application in Java
body: <p>I have a server application in Java, that holds a list of Student objects (implementing Serializable). The client application sends a message with an integer - index of Student object to fetch. Then the selected Student is sent to the client, the client modifies its value and sends back. The application however freezes at some point, and it's probably a problem with the lines I emphasized in the code below.</p>
  
  <p>Server:</p>
  
  <pre><code>public class Server {
  
      public static void main(String[] arg) {
          ArrayList &lt;Student&gt; studentList = new ArrayList &lt;Student&gt; ();
          studentList.add(new Student(170435, "justyna", "kaluzka", new ArrayList &lt;Float&gt;()));
          studentList.add(new Student(170438, "michal", "szydlowski", new ArrayList &lt;Float&gt;()));
          studentList.add(new Student(170436, "marek", "polewczyk", new ArrayList &lt;Float&gt;()));
          studentList.add(new Student(170439, "jakub", "szydlowski", new ArrayList &lt;Float&gt;()));
          studentList.add(new Student(170430, "anna", "majchrzak", new ArrayList &lt;Float&gt;()));
          studentList.add(new Student(170425, "krzysztof", "krawczyk", new ArrayList &lt;Float&gt;()));
          studentList.add(new Student(170445, "adam", "szydlowski", new ArrayList &lt;Float&gt;()));
          studentList.add(new Student(170415, "karol", "chodkiewicz", new ArrayList &lt;Float&gt;()));
          studentList.add(new Student(170465, "artur", "schopenhauer", new ArrayList &lt;Float&gt;()));
  
          ServerSocket socketConnection = null;
          ObjectInputStream serverInputStream = null;
          ObjectOutputStream serverOutputStream = null;
  
          try {
              socketConnection = new ServerSocket(11111);
              System.out.println("Server Waiting");
  
              Socket pipe = socketConnection.accept();
  
              serverOutputStream = new ObjectOutputStream( pipe.getOutputStream());
              serverInputStream = new ObjectInputStream( pipe.getInputStream());
  
              int index = serverInputStream.readInt();
              System.out.println(index);
  
              //  HERE'S WHEN THE PROBLEM STARTS
              serverOutputStream.writeObject(studentList.get(index));
              Student student = (Student) serverInputStream.readObject();
  
              System.out.println(student.toString());
          } catch (IOException e) {
              System.out.println(e);
          } catch (ClassNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } finally {
              try {
                  serverInputStream.close();
                  serverOutputStream.close();
                  socketConnection.close();
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      }
  }
  </code></pre>
  
  <p>Client: </p>
  
  <pre><code>public class Client {
  
  public static void main(String[] arg) {
          Student student = null;
          Socket socketConnection = null;
          ObjectOutputStream clientOutputStream = null;
          ObjectInputStream clientInputStream = null;
  
          try {
              socketConnection = new Socket("127.0.0.1", 11111);
  
              clientOutputStream = new ObjectOutputStream(socketConnection.getOutputStream());
              clientInputStream = new ObjectInputStream(socketConnection.getInputStream());
  
              clientOutputStream.writeInt(0);
              student = (Student) clientInputStream.readObject();
  
              student.setFamilyName("Konopnicka");
  
              clientOutputStream.writeObject(student);    
          } catch (Exception e) {
              System.out.println(e);
          } finally {
              try {
                  clientOutputStream.close();
                  clientInputStream.close();
                  socketConnection.close();
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      }
  }
  </code></pre>
  
  <p>My knowledge of client-server sockets is vague, so it's most likely a simple mistake. Any ideas?</p>
  
  <p>EDIT:
  Student class</p>
  
  <pre><code>public class Student implements Serializable {
      private static final long serialVersionUID = -5169551431906499332L;
      private int indexNumber;
      private String name;
      private String familyName;
      private ArrayList&lt;Float&gt; marks;
      private float average;
  
      public Student(int indexNumber, String name, String familyName,
              ArrayList&lt;Float&gt; marks) {
          this.indexNumber = indexNumber;
          this.name = name;
          this.familyName = familyName;
          this.marks = marks;
          this.average = 0;
          generateMarks();
          calculateAverage();
      }
  
      public int getIndexNumber() {
          return indexNumber;
      }
  
      public void setIndexNumber(int indexNumber) {
          this.indexNumber = indexNumber;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public String getFamilyName() {
          return familyName;
      }
  
      public void setFamilyName(String familyName) {
          this.familyName = familyName;
      }
  
      public float getAverage() {
          return average;
      }
  
      public void setAverage(float average) {
          this.average = average;
      }
  
      /**
       * Calculates average of all Student's marks.
       */
      public void calculateAverage() {
          float sum = 0;
          for (int i = 0; i &lt; marks.size(); i++) {
              sum += marks.get(i);
          }
          this.average = sum / marks.size();
      }
  
      /**
       * Generates a random set of marks for the student.
       */
      public void generateMarks() {
  
          for (int i = 0; i &lt; 10; i++) {
              addMark(new Random().nextFloat() * 5);
          }
  
      }
  
      /**
       * Mark getter
       * 
       * @return String representation of marks
       */
      public String getMarks() {
          String marksstr = "";
          for (int i = 0; i &lt; marks.size(); i++) {
              marksstr += marks.get(i).toString() + "   ";
          }
          return marksstr;
      }
  
      /**
       * Adds a mark to the list.
       * 
       * @param mark
       */
      public void addMark(float mark) {
          marks.add(mark);
      }
  
      @Override
      public String toString() {
          return "Index number:" + indexNumber + "\tName:" + name
                  + "\tFamily name:" + familyName + "\t\tAverage:" + getAverage()
                  + "\n";
      }
  }
  </code></pre>