2017-12-02 4 views
0

現在、クライアントが5つのランダムな整数のリストを作成してからクライアントに送信し、これを繰り返すまで、クライアント - サーバープログラムを作成しています。私はそれを止めるように言う。Java Client-Serverは適切な情報を送信し続けるサーバーを取得する

サーバはクライアントからリストを取得し、プライムである番号を見つけて、新しいリストに素数を入れて、クライアントからそれを送信し、クライアントがリストを送信している限り、それを繰り返す。

HERESにクライアントコード:

public class Client2 { 
    static boolean isRunning = false; 


public static void main(String[] args) throws Exception { 
    System.out.println("Running Client"); 

    Socket clientSocket = new Socket(InetAddress.getLocalHost(), 6789); 
    ObjectOutputStream toServer = 
      new ObjectOutputStream(clientSocket.getOutputStream()); 
    toServer.flush(); 

    ObjectInputStream inFromServer = 
      new ObjectInputStream(clientSocket.getInputStream()); 

    Random randint = new Random(); 
    Scanner input = new Scanner(System.in); 
    System.out.println("Enter “!” to startand stop, “#” to quit:"); 

    if(input.nextLine().equals("!")) { 
     isRunning = true; 
    } 

    Thread t = new Thread(new Runnable(){ 

     @Override 
     public void run() { 
      while(isRunning) { 
       List<Integer> randList = new ArrayList<Integer>(5); 
       //Makes the Random List 
       for(int i = 0; i < 5; i++) { 
        int num = randint.nextInt(98)+2; 
        randList.add(num); 
       } 

       //Sleeps the thread 
       try { 
        Thread.sleep(1000); 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

       //Writes the list 
       try { 
        toServer.writeObject(randList); 
        toServer.flush(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       System.out.println("Send: " + randList); 

       try { 
        System.out.println("Received: " + inFromServer.readObject()); 
       } catch (ClassNotFoundException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      }//End while loop 
      } 
    }); 
    Thread t2 = new Thread(new Runnable(){ 

     @SuppressWarnings("deprecation") 
     @Override 
     public void run() { 
      while(true) { 
       if(input.nextLine().equals("!")) { 
        t.suspend(); 
        System.out.println("Sleeping"); 
       } 
       if(input.nextLine().equals("!")) { 
        t.resume(); 
        System.out.println("Resuming"); 
       } 
      } 

      } 

    }); 
    t.start(); 
    t2.start(); 

} 
} 

そしてHERESにサーバコード:

public class Server { 
    static List clientNums = new ArrayList(); 
    static List primes = new ArrayList(); 
    public static void main(String[] args) throws Exception { 



    System.out.println("Running Server"); 
    ServerSocket welcome = new ServerSocket(6789); 

    Socket connectionSocket = welcome.accept(); 
    System.out.println("Connected"); 

    ObjectInputStream inFromClient = 
      new ObjectInputStream(connectionSocket.getInputStream()); 

    ObjectOutputStream toClient = 
      new ObjectOutputStream(connectionSocket.getOutputStream()); 
    toClient.flush(); 

    while(true) { 
     clientNums = (ArrayList) inFromClient.readObject(); 
     System.out.println("Client list: " + clientNums); 

     for(int i = 0; i < clientNums.size(); i++) { 
      if(isPrime((int) clientNums.get(i))) { 
       primes.add(clientNums.get(i)); 
      } 
     }//End for loop 

     System.out.println("Received: " + primes); 
     toClient.writeObject(primes); 
     primes.clear(); 
    }//End while loop 
}//End main 
public static boolean isPrime(int n) { //CITE: https://www.mkyong.com/java/how-to-determine-a-prime-number-in-java/ 
    for(int i=2;2*i<=n;i++) { 
     if(n%i==0) 
      return false; 
    } 
    return true; 

    } 
} 

今、すべてが、私はそれがしたいどのように動作します...実際EXCEPTインクルードクライアントが最初に送信しますリストでは、素数の最初のリストを正しく受信します。しかし、2番目以降のリストでは、新しいリストの代わりに最初の素数リストを受け取るだけです。サーバーが依然として素数のリストを正しく受信しているとはいえ、私はこれをちょっと悩まされてしまった。助けてもらえますか?相続人

クライアント用出力:

Running Client 
Enter “!” to startand stop, “#” to quit: 
! 
Send: [63, 63, 64, 4, 53] 
Received: [53] 
Send: [43, 6, 70, 67, 69] 
Received: [53] 
Send: [2, 29, 83, 45, 67] 
Received: [53] 

とサーバのための出力は:

Running Server 
Connected 
Client list: [63, 63, 64, 4, 53] 
Received: [53] 
Client list: [43, 6, 70, 67, 69] 
Received: [43, 67] 
Client list: [2, 29, 83, 45, 67] 
Received: [2, 29, 83, 67] 

答えて

1

あなたはObjectOutputStream.reset()またはObjectOutputStream.writeUnshared()を調査する必要があります。

+0

これはサーバーまたはクライアントでも実行する必要がありますか? – user2951723

+0

ありがとう! – user2951723

関連する問題