client-server exercise is stack
<p>Hello, in my program a client can add or search a contact.</p>

<p>If you want to search you give the surname and the server searches in an <code>ArrayList</code> to determine if the surname exists. If so, it sends back the contact, if not, it sends a message <code>NXRECORD</code>.</p>

<p>If you want to add a contact you give name, surname, job, address and phone and send it as an object to the server which adds it to its list.</p>

<p>The connection closes when the client wants to.</p>

<blockquote>
  <p>Error during I/O java.net.SocketException: Software caused connection
  abort: socket write error     at
  java.net.SocketOutputStream.socketWrite0(Native Method)   at
  java.net.SocketOutputStream.socketWrite(Unknown Source)   at
  java.net.SocketOutputStream.write(Unknown Source)     at
  java.io.ObjectOutputStream$BlockDataOutputStream.drain(Unknown Source)
    at
  java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(UnknownSource)
    at java.io.ObjectOutputStream.writeNonProxyDesc(Unknown Source)     at
  java.io.ObjectOutputStream.writeClassDesc(Unknown Source)     at
  java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)    at
  java.io.ObjectOutputStream.writeObject0(Unknown Source)   at
  java.io.ObjectOutputStream.writeFatalException(Unknown Source)    at
  java.io.ObjectOutputStream.writeObject(Unknown Source)    at
  Server.main(Server.java:35)</p>
</blockquote>

<p>Server code</p>

<pre><code>import java.net.*;
import java.util.ArrayList;
import java.io.*;

public class Server  {

public static void main(String[] args) {
    ArrayList &lt;PhoneBook&gt; listPhone=new ArrayList&lt;PhoneBook&gt;(); 

    PhoneBook a = new PhoneBook("a","a","","",1);
    listPhone.add(a);
    String strin,sName;
    boolean flag=false;
    int seat=0;
    try{

        ServerSocket server = new ServerSocket(5555,50);

        System.out.println("Waiting Incoming Connection...");
        System.out.println("Local Address :"+server.getInetAddress()+" Port :"+server.getLocalPort());
        Socket sock = server.accept();

        ObjectOutputStream out =new ObjectOutputStream(sock.getOutputStream()); 
        ObjectInputStream  in =new ObjectInputStream(sock.getInputStream()); 
        BufferedReader instream = new BufferedReader (new InputStreamReader (sock.getInputStream()));
        BufferedWriter outstream = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));


          strin=(String)in.readObject();

        do{

              if(strin.equals("START")){

                  out.writeObject("WAITING"); //line 35 where the exception occurs
                  out.flush();
                 }
            if(strin.equals("REQUEST_SEARCH")){
             sName=(String)in.readObject();
             for(int i=0;i&lt;listPhone.size();i++) 
                  if(listPhone.get(i).getsurName().equals(strin)){
                  flag=true;
                  seat=i;}
                   if(flag==true){
                       out.writeObject("RECORD"); 
                       out.flush();
                       out.writeObject(listPhone.get(seat).toString());
                       out.flush();
            }else{
                out.writeObject("NXRECORD");
                out.flush();
            }
         }else if(strin.equals("REQUEST_INSERT")){
             System.out.println("The contact is add");
                listPhone.add((PhoneBook)in.readObject());
         }

        }while(!strin.equals("END")); //bye = terminate the conversation
        out.writeObject("OK");
        out.flush();
        in.close();
        out.close();
        sock.close();
        System.out.println("Connection Closing...");
        }
        catch (Exception ex){
        System.out.println("Error during I/O");
        ex.getMessage();
        ex.printStackTrace();
        } } }
</code></pre>

<p>Client code</p>

<pre><code>import java.net.*;
import java.io.*;
import java.util.Scanner;

public class Client {

public static void main(String[] args) {

    try{
        Socket sock = new Socket("localhost", 5555);
        ObjectOutputStream out =new ObjectOutputStream(sock.getOutputStream()); 
        ObjectInputStream in =new ObjectInputStream(sock.getInputStream()); 
        BufferedReader instream = new BufferedReader (new InputStreamReader (sock.getInputStream()));
        BufferedWriter outstream = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));

        System.out.println("Sending Messages to the Server...");
        System.out.println("Connecting to "+ sock.getInetAddress()+ " and port "+sock.getPort());
        System.out.println("Local Address :"+sock.getLocalAddress()+" Port:"+sock.getLocalPort());
        String strin, strout;
        String surName,choice;
        int ch;
        boolean flag=false;
        Scanner input = new Scanner(System.in);


        do{     
            out.writeObject("START");
            out.flush();
            strin=(String)in.readObject();

            if(strin.equals("WAITING")){
                System.out.println("Prees 1 if you want to search a contact");
                System.out.println("Prees 2 if you want to add a contact");
                ch=input.nextInt();
                if(ch==1){
                    out.writeObject("REQOUEST_SEARCH");
                    out.flush();    
                    System.out.print("Give surname of contact :");
                    surName=input.next();
                    out.writeObject(surName);
                    out.flush();
                    strin=(String)in.readObject();

                    if(strin.equals("RECORD")){
                         System.out.println(in.readObject());
                    }else if(strin.equals("NXRECORD")){
                    System.out.println("The contact is not exist!!");   
                    }
                }else if(ch==2){
                    out.writeObject("REQOUEST_INSERT");
                    out.flush();    
                    System.out.print("Write the contact elements "); 
                       System.out.print("Write the name: "); 
                       String name = input.next(); 
                       System.out.print("Write the surname: "); 
                       surName = input.next(); 
                       System.out.print("Write the job: "); 
                       String job = input.next(); 
                       System.out.print("Write the street: "); 
                       String street = input.next(); 
                       System.out.print("Write the phone number: "); 
                        int number = input.nextInt(); 
                        PhoneBook p=new PhoneBook(name,surName,job,street,number);
                        out.writeObject(p);
                        out.flush();        
                   }
                }   
            System.out.print("If you want to EXIT!! pres Y"); 
            choice = input.next();

        }while(!choice.equals("Y"));
        out.writeObject("END");
        out.flush();
        strin=(String)in.readObject();
        if(strin.equals("OK"))  
        in.close();
        out.close();
        sock.close();
        System.out.println("Connection Closing...");
        }
        catch (Exception ex){
        System.out.println("Connection Refused!!!");
        }
        }
        }
</code></pre>
