2015-11-23 38 views
0

我在客戶端和服務器之間迭代傳輸ArrayList<Object>時遇到問題。事實上,我必須在我的客戶端收到名單,並在第一次嘗試它的工作,但是當我在程序的Home界面中返回時,我重試發送List它啓動java.net.SocketException: Socket closed。我認爲我的問題可能是錯誤地關閉了我的數據/對象流或關閉了Sever套接字。我錯了嗎?錯誤java.net.SocketException:在客戶端 - 服務器通信中關閉套接字

這裏是我的客戶端代碼:

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); 
      } 
     } 

這裏是服務器代碼:

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); 
    } 
} 

回答

0

關閉套接字的輸入流或輸出流關閉套接字。

解決方案:不要,直到你完成套接字。

+0

我試過並給了我一個EOFException。 –

+0

如果你有'EOFException'對方關閉了套接字,或者至少關閉它以輸出。所以很難看到你試過的對應於'不'。 – EJP

+0

好吧我已經按照你的說法(我沒有看到一個ObjectOutputStream在服務器中關閉),但現在,當我嘗試第二次啓動方法時,它停止工作並崩潰。 –

相關問題