java timer and socket problem

╄→гoц情女王★ 提交于 2019-12-29 09:10:38

问题


I'm trying to make a program which listens to the client input stream by using socket programming and timer

but whenever timer executes.. it gets hanged

Please help me out

here is the code...

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
    // TODO add your handling code here:
    try

    {
        ServerUserName=jTextField1.getText();
        ss=new ServerSocket(5000);
        jButton1.enable(false);
        jTextArea1.enable(true);
        jTextField2.enable(true);
        Timer t=new Timer(2000, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                try
                {
                    s=ss.accept();                    
                    InputStream is=s.getInputStream();
                    DataInputStream dis=new DataInputStream(is);
                    jTextArea1.append(dis.readUTF());

                }
                catch(IOException IOE)
                {
                }
                catch(Exception ex)
                {
                    setLbl(ex.getMessage());
                }

            }
        });
        t.start();
    }
    catch(IOException IOE)
    {

    }
}

Thanks in advance


回答1:


Make the program multi-threaded; one thread listens on the socket, the other one handles the GUI. Use SwingUtilities.invokeLater to let the GUI thread ("event dispatching thread") do the GUI updates whenever the network thread receives data.




回答2:


Every call to accept waits for a new client to connect to the server. The call blocks until a connection is established. It sounds like you have a single client that maintains a connection to the server.

One solution is to pull

s=ss.accept();                    
InputStream is=s.getInputStream();
DataInputStream dis=new DataInputStream(is);

outside of the timer portion of the code.

Update: Be aware though that readUTF is still going to block if there is no data available to be read.




回答3:


I think you want to use socket timeouts instead of a timer:

Thread listener = new Thread() {
    ServerSocket ss;

    @Override
    public void run() {
        try {
            ss = new ServerSocket(5000);
            ss.setSoTimeout(2000);
            try {
                while (true) {
                    try {
                        final String text = acceptText();
                        SwingUtilities.invokeLater(new Runnable() {
                            public void run() {
                                jTextArea1.append(text);
                            }
                        });
                    } catch (final Exception ex) {
                        SwingUtilities.invokeLater(new Runnable() {
                            public void run() {
                                setLbl(ex.getMessage());
                            }
                        });
                    }
                }
            } finally {
                ss.close();
            }
        } catch (IOException ex) {
            Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private String acceptText() throws IOException {
        Socket s = ss.accept();
        try {
            InputStream is=s.getInputStream();
            try {
                DataInputStream dis=new DataInputStream(is);
                return dis.readUTF();
            } finally {
                is.close();
            }
        } finally {
            s.close();
        }
    }
};
listener.setDaemon(true);
listener.start();


来源:https://stackoverflow.com/questions/2673045/java-timer-and-socket-problem

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!