本帖最后由 king3258267 于 2010-11-30 18:04:14 编辑

解决方案 »

  1.   

    要开一个线程来 接受消息。
    public class ServerGUI extends javax.swing.JFrame implements Runnable   private AtomicBoolean startState = new AtomicBoolean(false);    public void run() {
            BufferedReader bf = null;//定义一个BufferedReader类型的读内容的引用
            InputStreamReader inputSR = null;//定义一个可读输入流
            try {
                welcomeSocket = new ServerSocket(Integer.valueOf(monitor_port.getText()));
                assert this.welcomeSocket.isBound();
                if (this.welcomeSocket.isBound()) {
                    System.out.println("SERVER inbound data port "
                            + this.welcomeSocket.getLocalPort()
                            + " is ready and waiting for client to connect...");
                }
            } catch (SocketException e) {
                System.out.println("Unable to create socket.");
            } catch (IOException ioe) {
                System.out.println("Unable to read data from an open socket.");
            }        try {
                while (true) {
                    Socket connectionSocket;
                    connectionSocket = welcomeSocket.accept();
                    //System.out.println("新连接建立!" + connectionSocket.getInetAddress());
                    inputSR = new InputStreamReader(connectionSocket.getInputStream());
                    BufferedReader inFromClient = new BufferedReader(inputSR);
                    receive_Contents.append(inFromClient.readLine());
                    inputSR.close();
                    inFromClient.close();
                    connectionSocket.close();
                }
            } catch (IOException e) {
                System.out.println("Unable to read data from an open socket.");
            } finally {
                try {
                    welcomeSocket.close();
                } catch (IOException e) {
                    System.out.println("Unable to close an open socket.");
                }
            }
        }
        private void start_MonitorActionPerformed(java.awt.event.ActionEvent evt) {
            if (startState.compareAndSet(false, true)) {
                Thread t = new Thread(this);
                t.setDaemon(true);
                t.start();
            }
        }
      

  2.   

    你知道swing是单线程吧。他一条原则是只能在事件分发线程(EDT)中访问组件,这是为了线程安全考虑。所以swing是靠EDT驱动的,所以所有的事都是他来完成的。但如果EDT阻塞,那么其他的事件什么的就不能响应,界面就会卡住。我怀疑,你在EDT中开启服务监听,因为如果你没有特殊处理,监听是阻塞的。所以就会导致界面假死。