0

I have a problem with the iterated transfer of ArrayList<Object> between client and server. Infact I have to receive in my client the List and the first time i try it works, but when i return in the program's Home interface and i retry to send the List it launch a java.net.SocketException: Socket closed. I think that my problem could probably be the erroneous closing of my data/object flux or the closing of the Sever Socket. Am i wrong?

Here is my Client code:

private final static int PORT = 6543;
Socket s = null;
DataInputStream in;
DataOutputStream out;
ObjectOutputStream outObj;
ObjectInputStream inObj;
private List<Comunication> pfList = new ArrayList<Comunication>();


public Socket Connect() throws IOException{


                System.out.println("Provo a connettermi al server....");
                s = new Socket("192.168.1.3",PORT);  

                System.out.println("Connesso.");

                //in = new DataInputStream(s.getInputStream());
                out = new DataOutputStream(s.getOutputStream());
                inObj = new ObjectInputStream(s.getInputStream());
                //outObj = new ObjectOutputStream(s.getOutputStream());

   return s;
}

public void getZanzIn() throws IOException{
    int i;

    System.out.println("Entro in prova");
    try {
        System.out.println("Dentro prova prima del flusso");

        pfList = (ArrayList<Comunication>)inObj.readObject();

        System.out.println("Dentro prova dopo il flusso");

        inObj.close();
        //s.close();

    } catch (IOException | ClassNotFoundException ex) {
        Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);

    }

    for (Communication pfList1 : pfList) {
        System.out.println(pfList1.getIdOrdine()+ " " + pfList1.getNome() + " " + pfList1.getTipo() + " " + pfList1.getRiferimento()+" "+pfList1.getAltezza()+" "+pfList1.getLarghezza()+" "+pfList1.getTipoMisura()+" "+pfList1.getData()+" "+pfList1.getColore()+" "+pfList1.getAttaccoFrontale()+" "+pfList1.getFrizione()+" "+pfList1.getStatoTelaio()+" "+pfList1.getStatoRete()+" "+pfList1.getStatoLavorazione());
    }
    pfList.clear();

}

public void CommunicateServer () {


            try {
                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                String str = "getZanzCut";
                System.out.print(">> ");
                out.writeUTF(str);
                out.flush();
                //str2=in.readUTF();
                //System.out.println("Server says: "+str2);


                out.close();
                //s.close();

            } catch (IOException ex) {
                Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

Here is the server code:

public class Server {

public final static int PORT = 6543;
ServerSocket ss = null;
Socket s = null;
DataInputStream in;
DataOutputStream out;
ObjectInputStream objIn;
ObjectOutputStream objOut;
Connection conn;
protected final static String NOMEDRIVER = "com.mysql.jdbc.Driver";
protected final static String SERVERURL =  "jdbc:mysql://localhost:3306/datazanzariere?zeroDateTimeBehavior=convertToNull";
protected final static String USER = "root";
protected final static String PASSWORD = "admin";


public Socket WaitObj(){
    try {
       System.out.println("inizializzo il server");
       ss = new ServerSocket(PORT);
       System.out.println("server pronto in ascolto");
       s = ss.accept();
       System.out.println("connessione stabilita");
       in = new DataInputStream(s.getInputStream());
       objOut = new ObjectOutputStream(s.getOutputStream());

    } catch (IOException ex) {
       Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
    }
   return s;
}

public void CommunicateClient(){
   try {
       BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       String str="";

        str=in.readUTF();
        System.out.println("client says: "+str);
        System.out.print(":: ");
        if(str.equals("getZanzCut")){
            this.getZanzCut();
        }

        in.close();
        //s.close();
        //ss.close();
   } catch (IOException ex) {
       Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
   }
}


public void getZanzCut(){
   try {
       List<Comunication> comList = new ArrayList();
       Class.forName(NOMEDRIVER);
       conn = DriverManager.getConnection(SERVERURL, USER, PASSWORD);
       Statement st = conn.createStatement();
       Statement st2 = conn.createStatement();
       Statement st3 = conn.createStatement();
       Statement stm4 = conn.createStatement();
       Statement stm5 = conn.createStatement();
       st.execute("Create view Modello (ID, Tipo) as select ZanzarieraV, Tipo from verticale union all select Zanzariera, Tipo from laterale");
       st2.execute("CREATE view DatiCliente (ID, Nome) as SELECT ClienteF, Cognome FROM personafisica UNION SELECT Cliente, NomeAzienda FROM personagiuridica");
       ResultSet rs = st3.executeQuery("SELECT IdOrdine, D.Nome, Tipo, Riferimento, Larghezza, Altezza, TipoMisura, Data, C.Colore, Frizione, AttaccoFrontale\n" +
               "FROM riepilogoordine R JOIN Colore C ON R.Colore = C.IdColore\n" +
               "JOIN Modello M ON R.ZanzarieraO = M.ID \n" +
               "JOIN DatiCliente D ON R.ClienteO = D.ID\n" +
               "WHERE StatoRete = 'Da tagliare'");

       while(rs.next()) {
           Comunication com = new Comunication();
           com.setIdOrdine(rs.getInt("IdOrdine"));
           com.setNome(rs.getString("Nome"));
           com.setTipo(rs.getString("Tipo"));
           com.setRiferimento(rs.getString("Riferimento"));
           com.setLarghezza(rs.getInt("Larghezza"));
           com.setAltezza(rs.getInt("Altezza"));
           com.setTipoMisura(rs.getString("TipoMisura"));
           com.setData(rs.getDate("Data"));
           com.setColore(rs.getString("Colore"));
           com.setFrizione(rs.getString("Frizione"));
           com.setAttaccoFrontale(rs.getString("AttaccoFrontale"));

           comList.add(com);   
       }
       objOut.writeObject(comList);
       stm4.execute("drop view modello");
       stm5.execute("drop view DatiCliente");
       comList.clear();
       conn.close();
       objOut.close();
       //s.close();
       //ss.close();
   } catch (ClassNotFoundException | SQLException | IOException ex) {
       Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
   }
}

1 Answers1

0

Closing the input stream or output stream of a socket closes the socket.

Solution: don't, until you're done with the socket.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I've tried and gave me an EOFException. – m.gravagno Nov 23 '15 at 12:27
  • If you got an `EOFException` the peer closed the socket, or at least shut it down for output. So it's hard to see what you tried that corresponds to 'don't'. – user207421 Nov 23 '15 at 12:36
  • Ok I've done as you say(I didn't see an ObjectOutputStream was closed in server) , but now, when i try to launch for the second time the methods, it stop working and crash. – m.gravagno Nov 23 '15 at 12:43
  • Only you know what that means. If you have a proper question, ask it. That isn't one. – user207421 Nov 23 '15 at 12:54