ループに問題があります。私のプロジェクトでは、マッチからソケットを介してコメントを送信しています。クライアントの最後のコメントがまだ出力されているため、ループの 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;
}
}