【问题标题】:Client stopps reacting to server's messages客户端停止响应服务器的消息
【发布时间】:2012-05-15 19:11:51
【问题描述】:

我遇到了一个新问题。 我正在用 Java+Swing 编写一个 Messenger,我选择了这种方式来进行客户端与服务器的对话:

我的课程正在运行(所有代码都是针对客户端的) (服务器没问题 - 我检查过)

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

}

我主要做一个变量

  run = new Running();

然后我将这个变量发送到任何地方,它似乎工作。 它提供 readline 或 writeline throwgh 套接字。 我可以注册用户、登录或添加联系人。

我可以打开 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();
    }

}

当然行!=""; 可能是线程的一些 Swing 问题? Swing 无法处理我的请求?

瓦西里。

【问题讨论】:

  • 1) 将您的问题分成两个 - 三个分开的问题 2) 在这个论坛上搜索,我可以在这里找到一些关于 Swing 和 Socket 以及 SwingWorker / Runnable#Thread 的非常有趣的代码

标签: java swing sockets concurrency timer


【解决方案1】:

在我看来问题在于这一行,您的 PrintStream 未设置为自动刷新,因此尝试更改此行:

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

到这里

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

所以它不会将输入存储到缓冲区,而是会自动将其刷新。欲了解更多信息PrintStream(OutputStream out, boolean autoFlush)

【讨论】:

    【解决方案2】:

    嘿,他说的是对的,该字符串将使用 /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) {}
    }
    

    【讨论】:

    • 关于问题的新有趣信息(来自调试)!查看我的问题的底部
    【解决方案3】:

    这可能是因为 readLine 方法希望该行以 \n 结尾。尝试让服务器发送消息并在其末尾添加一个 \n

    【讨论】:

    • 服务器发送消息 outputStream.println("some string");所以他们似乎以 \n 结尾,因为 println
    • 尝试用这样的 main 创建一个小测试类:for(;;) running.receiveLine() 如果它不起作用,请查看代码在客户端中的停止位置
    • 没用。我可以在第一次打开消息框时发送 2 或 2000 条消息,一切正常。但是当我关闭它并第二次打开时 - 我只收到了 50% 的消息。
    • 可能是服务器的问题。如果您无法识别错误,请尝试使用诸如 wireshark 之类的东西来查看流量是否实际上是由服务器发送的,以及消息是否正是您所期望的
    • 关于问题的新有趣信息(来自调试)!查看我的问题的底部
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-23
    • 1970-01-01
    • 1970-01-01
    • 2014-02-28
    • 2019-07-16
    • 2012-11-09
    • 2012-12-05
    相关资源
    最近更新 更多