0

新たな問題に直面しました。私はJava + Swingでメッセンジャーを書いていますが、クライアントとサーバーとの会話を行うためにその方法を選択しました:

私はクラスを実行しています(すべてのコードはクライアント用です)(サーバーは問題ありません-チェックしました)

public class Running {
private Socket s;
private PrintStream ps;
private BufferedReader br;

public Running(){
try{
    s = new Socket(InetAddress.getLocalHost(), 8072);
    ps = new PrintStream(s.getOutputStream());
    br = new BufferedReader(new InputStreamReader(s.getInputStream()));
} catch (UnknownHostException ex) {
    System.out.println("11");
    ex.printStackTrace();
} catch (IOException ex) {
    System.out.println("00");
    ex.printStackTrace();
}
}

public String receiveLine(){
    String ret = "";
    try {
        ret = br.readLine();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return ret;
}

public void sendLine(String s){
    ps.println(s);
}

public void close(){
    try {
        s.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

そして主に私は1つの変数を作ります

  run = new Running();

次に、この変数をどこにでも送信すると、うまくいくように見えました。readline または writeline スロー ソケットを提供します。ユーザーを登録したり、ログインしたり、連絡先を追加したりできました。

MessageFrame を開いて、連絡先にメッセージを送信できます。しかし、それを閉じると、プログラムがサーバーのメッセージに正しく反応しなくなります。メッセージの 30 ~ 70% にしか反応しません。確認しました - サーバーは問題ありません。したがって、問題は Running または MessageFrameListener にあります

public class MessageFrameListener{
private MessageFrame mf;
private User us;
private Contact cn;
private Timer timer;
private Running run;

public MessageFrameListener(MessageFrame m_f, User u_s, Contact c_n, Running r_n){
    run = r_n;
    mf = m_f;
    us = u_s;
    cn = c_n;
    m_f.addButtonListener(new SButtonListener());
    m_f.addWinListener(new FrameListener());
    timer = new Timer(500,new timerListener());
    timer.start();
}


public class timerListener implements ActionListener{
    public void actionPerformed(ActionEvent e) {
                SwingWorker<String,Void> sw = new SwingWorker<String,Void>(){
                    public String doInBackground() {
                        String ret = "";
                        ret = run.receiveLine();
                        return ret;
                    }
                    public void done() {
                        String[] results = null;

                            try {
                                results = get().split(" ");
                            } catch (InterruptedException
                                    | ExecutionException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }


                        if("m".equals(results[0])){
                            if("-1".equals(results[2]))
                                mf.addLine2("Error");
                            else{
                                mf.addLine2(results[3]);
                            }
                        }


                    }
                };
                sw.execute();
    }
}

public class SButtonListener implements ActionListener{
    public void actionPerformed(ActionEvent e) {
            String insert = mf.getInput();
            if(!insert.equals("")){
                String infoString = "m "+us.getName()+" "+cn.getName()+" "+insert;
                run.sendLine(infoString);
                mf.addLine(insert);
                mf.refreshInput();
            }
    }
}


public class FrameListener implements WindowListener{

    @Override
    public void windowClosing(WindowEvent e) {
        timer.stop();
        timer = null;
        mf.close();
    }

新情報!デバッグを開始しました。私のクライアントはサーバーからメッセージを受け取り、この行に到達します

   mf.addLine2(results[3]);

しかし、その後何も起こりません!Swing は改行を追加しません。

addLine2 のコード:

  public void addLine2(String line){
    //dialogArea.append(line+"\n");

    try { 
        if(line != ""){
            String formLine = us.getName()+" ("+now()+")\n";
            doc.insertString(doc.getLength(), formLine+line+"\n",st2);
        }

    }   
    catch (BadLocationException e){
        e.printStackTrace();
    }

}

もちろん line!=""; スレッドに関する Swing の問題でしょうか? そして、Swing は私の要求を処理できませんか?

ヴァシリー。

4

3 に答える 3

2

問題はこの行にあるようです.PrintStreamは自動的にフラッシュするように設定されていないため、この行を変更してみてください:

ps = new PrintStream(s.getOutputStream());

これに

ps = new PrintStream(s.getOutputStream(), true);

そのため、入力をバッファに保存する代わりに、自動的にフラッシュします。詳細については PrintStream(OutputStream out, boolean autoFlush)

于 2012-05-16T03:41:05.583 に答える
1

これは、readLine メソッドが行が \n で終わることを想定していることが原因である可能性があります。サーバーにメッセージとその末尾の \n を送信させてみてください

于 2012-05-15T19:28:02.317 に答える
1

彼の言っていることは正しいです。その文字列は /n で実行されますが、取得しようとしています。

in that just try with  **While** condition , that till true get the string .

ここにサンプルがあります、

try {
        while(true) {

            DataInputStream is = new                      DataInputStream(sock.getInputStream());
          System.out.println("" +is.readLine());

            line =is.readLine();

        } // end of while
    } catch(Exception ex) {}
}
于 2012-05-15T19:45:47.990 に答える