2013-06-15 7 views
0

루프에 문제가 있습니다. 내 프로젝트에서 나는 소켓에서 소켓을 통해 의견을 보내고있다. 고객의 마지막 코멘트가 여전히 인쇄되어 있기 때문에 무언가 잘못되었습니다.Client에서 while 루프가 잘못되었습니다.

 PrintWriter out = new PrintWriter(socket.getOutputStream(), true); 

     while(true){ 
     out.println(rel.getCommentary()); 

     } 

getCommentary는

고객의 루프

 in = new Scanner(socket.getInputStream());   
     while(in.hasNextLine()){ 
      showCommentary(in.nextLine()); 
     } 

서버 GUI

public void actionPerformed(ActionEvent e) { 

     Object source = e.getSource(); 

     if(source == addComment){ 

      String comment=commentField.getText(); 
      rel.setCommentaryText(comment); 
      panel4.setBackground(Color.green); 

     } 

클라이언트

private void showCommentary(String text){ 
    showArea.append(text+ "\n"); 
    showArea.setCaretPosition(showArea.getDocument().getLength()); 
} 
의 JTextField에서 현재 코멘트와 방법

관계 클래스

public class Relation{ 

    String relation; 

    public Relation() { 

    } 

    public void setCommentaryText(String relation){ 
     this.relation= relation; 
    } 

    public String getCommentary(){ 
     return this.relation; 
    } 

} 
+0

입력과 출력을 추가하십시오. – darijan

+1

인쇄를 위해 서버 사이트에서 루프를 사용하는 이유는 무엇입니까? –

+0

내가 내 actionPerformed – Dominik

답변

0

음, Server, 당신은 논평을 인쇄하는 방법이 있어야합니다

if (source == addComment) { 
    String comment= commentField.getText(); 
    //call the server's print method we just wrote, only once, no loop! 
    server.printCommentary(comment); 
} 

: 당신은 논평을 설정할 때,

public void printCommentary(String commentary) { 
    out.println(commentary); 
} 

ServerGUI에서을 귀하의 버그는 while(true) 루프가 항상 실행되고 있다는 것입니다. 마지막 해설을 출력 스트림에 영원히 인쇄합니다. 너는 그렇게해서는 안된다.

+0

서버에서 나는 setCommentaryText() 클래스 Relation에서 rel.getCommentary()를 사용하고 있습니다. serverGUI에서 rel.setCommentaryText (comment)를 얻었습니다. 어디에서 commentField에서 텍스트를 설정 – Dominik

+0

그래서 나는 서버에 다른 방법이 필요 없거나 틀렸다고 생각합니다. – Dominik

+0

틀렸어. 서버에서 보내기에는'while (true)'루프가 필요하지 않지만, 답안에서 쓰듯이 한 번 수행하는 메서드가 필요합니다. – darijan