0

ループに問題があります。私のプロジェクトでは、マッチからソケットを介してコメントを送信しています。クライアントの最後のコメントがまだ出力されているため、ループの 1 つに何か問題があることがわかっています。

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

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

        }

getCommentary は、JTextField からの現在のコメントを持つメソッドです。

クライアントのループ

        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());
}

関係クラス

public class Relation{

    String relation;

    public Relation() {

    }

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

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

}
4

1 に答える 1

0

では、Serverコメントを出力するメソッドが必要です。

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

ServerGUIで注釈を設定すると、次のようになります。

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

あなたのバグは、while(true)ループが常に実行されていることです。最後のコメントを永久に出力ストリームに出力します。あなたはそれをすべきではありません。

于 2013-06-15T11:01:38.270 に答える