2017-12-02 12 views
0

현재 클라이언트가 연속적으로 5 개의 임의의 정수 목록을 작성한 다음 클라이언트에게 보내고 이것을 계속 반복 할 때까지 클라이언트 - 서버 프로그램을 만들고 있습니다. 나는 멈추라 고 말한다.Java 클라이언트 - 서버 올바른 정보를 계속 전송하도록 서버를 얻으려고합니다.

서버는 클라이언트로부터 목록을 가져 와서 어떤 숫자가 소수인지 알아 내고 소수를 새 목록에 넣은 다음 클라이언트에서 보낸 다음 클라이언트가 목록을 보내는 한 그것을 반복합니다.

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; 

    } 
} 

이제 모든 사실를 제외하고 내가 ...가 원하는 방법을 작동 목록에서, 그것은 소수의 첫 번째 목록을 올바르게 수신 할 것입니다. 그러나 두 번째 목록과 그 이후의 목록에서는 새로운 목록 대신 소수의 첫 번째 목록을 계속 수신합니다. 심지어 서버가 여전히 소수를 올바르게 수신하고 목록을 작성하고 있다고 생각합니다. 나는 이것에 조금 비틀어졌다. 누구든지 도와 줄 수 있습니까? Heres는

클라이언트에 대한 출력 :

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