2015-11-23 37 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在服务器中关闭),但现在,当我尝试第二次启动方法时,它停止工作并崩溃。 –

相关问题