2016-10-22 30 views
0

你好,我在我的應用程序java上有一個錯誤,這個應用程序發送客戶端到服務器的值,服務器執行2的權力收到價值,並返回給客戶端,一切正常,但是一旦服務器不再接收數值,它只會播放一次,我不明白問題所在,我該如何解決這個問題?代碼如下錯誤應用程序java客戶端-server

客戶:

import java.io.*; 
import java.net.*; 

public class Client{ 

    public static void main(String args[]){ 

      try{ 

      //controllo che i valori siano corretti  
      /* if(args.lenght != 2){ 
       System.out.println(" Errore la sintassi "); 
       System.exit(1); 
       } 
      */ 
      String host=""+args[0]; 
      int port=Integer.parseInt(args[1]); 
     // System.out.println("\n Scriverò sul socket "+host); 
     // System.out.println("\n Porta: "+port);  

      //leggo valori da input 

      BufferedReader input=new BufferedReader(new InputStreamReader(System.in));   

      System.out.println("\n Inserisci un valore (CLIENT): "); 
      String lettura=input.readLine(); 

      while(!lettura.equals("fine")){ 

      System.out.println("\n Inserisci un valore (CLIENT): "); 
      Socket s=new Socket(host,port); 
      PrintWriter output=new PrintWriter(s.getOutputStream()); 
      output.println(lettura); 
      output.flush(); 

      //attendo dal server il risultato 
      BufferedReader insok = new BufferedReader(new InputStreamReader(s.getInputStream()));  
      String lettsock=insok.readLine(); 
     //System.out.println(" Ho ricevuto dal server il valore "+insok.readLine()); 
      System.out.println("\n Ho ricevuto dal server : "+lettsock); 

      lettura=input.readLine();  
     } 

      // System.out.println("\n Client Terminato con successo \n"); 

      //chiusura input 
      input.close(); 
     } 

     catch(Exception ex){ 
      System.out.println("\n Errore: " + ex); 
      System.exit(2); 
     } 
    } 


} 

SERVER:

import java.io.*; 
import java.net.*; 


public class Server{ 

    static final int PORT = 1024; 

    public static void main(String args[]){ 

    try{ 

     ServerSocket ss=new ServerSocket(PORT); 

      while(true) { 
       Socket ns = ss.accept(); 
       BufferedReader networkIn = new BufferedReader(new InputStreamReader(ns.getInputStream())); 
       PrintWriter networkOut=new PrintWriter(ns.getOutputStream()); 
       String line; 
       System.out.println("\n Attendo valori \n "); 
       while ((line = networkIn.readLine()) != null) { 

        System.out.println("Ricevuto: " + line); 
        try{ 
        int number=Integer.parseInt(line); 
        number=number*number; 
        networkOut.println(number); 
        networkOut.flush(); 
        } 
        catch(Exception ex){ 
        System.out.println("\n Non mi hai inviato un numero \n"); 
        } 
       } 

       ns.close(); 
      } 


    } 
    catch(Exception ex){ 

     System.out.println("\n Errore Server: "+ex); 
     System.exit(2); 
    } 


    } 

} 

回答

0

這可能是一個事實,即客戶端打開插座while做循環,然後不關閉它。
服務器不知道套接字尚未關閉,因此仍在等待來自它的輸入。

嘗試將客戶端連接代碼(下)移至while循環之前。

Socket s=new Socket(host,port); 
PrintWriter output=new PrintWriter(s.getOutputStream()); 
BufferedReader insok = new BufferedReader(new InputStreamReader(s.getInputStream())); 

而且,您的服務器的while循環要真有退出的一種方式,因爲目前它可以退出的唯一方法是,如果它拋出一個異常,這是不理想...... 也許讓這個當客戶端發送消息'close'時,它斷開連接。

+0

謝謝你的開始 –