-1

Possible Duplicate:
java timer and socket problem

So here is the code. What I want to do - is client loop with server, to get messages from server, which sends them sometimes. Here is attempt with timer.

private MessageFrame mf;
private User us;
private Contact cn;

private Socket s;
private PrintStream ps;
private BufferedReader br;

private Timer timer ;

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

}

public void init(){
    try {
        s = new Socket(InetAddress.getLocalHost(), 8072);
        ps = new PrintStream(s.getOutputStream());
        br = new BufferedReader(    new InputStreamReader(s.getInputStream()));
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    timer.start();      

}

public class timerListener implements ActionListener{
    public void actionPerformed(ActionEvent e) {
            //String insert = mf.getInput();

                String result;
                try {
                    //проверить, что буфер не пуст
                    if((result = br.readLine()) != null){


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

                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }



    }
}

But when I run it - the program stops reacting to my actions. I can't write a text, or press a button.

Community
  • 1
  • 1
146 percent Russian
  • 2,016
  • 2
  • 14
  • 20
  • 2
    You are probably running your socket on the Event Dispatching Thread (EDT) and you should probably move that to a [SwingWorker](http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingWorker.html) – Guillaume Polet May 14 '12 at 18:18
  • 1
    @GuillaumePolet That should probably be an answer. Considering that it's the correct answer. – trutheality May 14 '12 at 18:22
  • 1
    It's that one-in-every-thirty questions on SO which have the exact same solution :) – Marko Topolnik May 14 '12 at 18:24
  • @trutheality I don't have sufficient information to make that an affirmation and thus an answer. So I figured I could tip the OP into the good direction. I think I just posted an answer for freezing GUI with Socket this same morning. So from there, and if it is his problem, he should find his way. – Guillaume Polet May 14 '12 at 18:28
  • You may find some useful information [here](http://stackoverflow.com/questions/10575808/java-threads-swing-and-serversocket/10575855#comment13714217_10575855) – Guillaume Polet May 14 '12 at 18:30
  • 1
    `//проверить, что буфер не пуст` code comments in English, please. Google translate tells me that means "check that buffer is not empty". (You could have used the translate service before posting, if not sure on the translation.) – Andrew Thompson May 14 '12 at 18:49

2 Answers2

1

Read calls to BufferedReader are blocking, Run them in a separate thread preferably a swing worker. Your readLine() call is causing the main event thread to hang which means no other events are propagating.

To confirm this put a break in the TimerActionListener and investigate the running thread stack or just SIGQUIT and thread dump. It should be pretty clear what thread the read call is causing to block.

nsfyn55
  • 14,875
  • 8
  • 50
  • 77
0

I believe your br.readline() call blocks until you get data from your InputStream. If nothing is coming over the socket to read, the code will just hang there.

You may want to see the thread over here: Asynchronous IO in Java?

Community
  • 1
  • 1