请看代码 
不知为何只要建立Socket连接,就会阻塞住,界面出不来。
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;public class ServerForm extends javax.swing.JFrame {
    private ServerSocket serverSocket;
    /** Creates new form ServerForm */
    public ServerForm() {
        initComponents();
       
    }    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(86, 86, 86)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(93, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(104, Short.MAX_VALUE))
        );        pack();
    }// </editor-fold>                            /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        new ServerForm().setVisible(true);
        System.out.println("oh ~~yeah!");
        
        try {
            ServerSocket ss = new ServerSocket(7777);
         
            while (true) {
                Socket s = ss.accept();
                new ServerThread(s).start();
            }
           
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
  
        System.out.println("oh ~~no!");
    }    // Variables declaration - do not modify                     
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration                   }

解决方案 »

  1.   

    没看出错误来,至少这段没错。这种直接debug呀,学会debug,能解决不少问题。
      

  2.   

    ServerThread,本想给你调下,但这个东西是个啥?
      

  3.   

    这个是serverthread~~~每次运行~~socket连接倒是建立了~但是界面出不来~~不知为何~~求解/*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package com.zhang7nan;import java.io.*;
    import java.net.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;/**
     *
     * @author Administrator
     */
    public class ServerThread extends Thread {    Socket s;
        OutputStream os = null;
        InputStream is = null;    public ServerThread(Socket s) {
            this.s = s;
        }    @Override
        public void run() {        try {
                os = s.getOutputStream();
                is = s.getInputStream();            //send welcome msg
                os.write("welcome to connect the server!".getBytes());
                byte[] bytes = new byte[1024];
                int k = is.read(bytes);
                System.out.println(new String(bytes, 0, k));
            } catch (IOException ex) {
                Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
            }
        }    public void cut() {
            try {
                is.close();
                os.close();
                s.close();
            } catch (IOException ex) {
                Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
      

  4.   

    界面就是Swing做的啊~~我用Netbeans
    问题就是每次运行的时候Socket服务器可以建立~但是SWing写的界面就出不来~
    我觉得有可能是阻塞了~但是我找不到问题的根本~
      

  5.   

    Socket对IO的读写操作,要自己创建线程来进行。
    不要在Swing的事件模型中进行。
    要学会使用多线程技术,进行swing的编程工作。
      

  6.   

    我是把对IO的读写放在ServerThread里了啊~~
    ServerForm里没有IO操作~
      

  7.   

    当然会阻塞,SWING默认情况下都是单线程的,你要通信必须用多经程。
      

  8.   

    多看两个例子就OK了这个东西研究起来说深就深,说浅也浅建议查看ibm 的socket教程
      

  9.   

    重新启动一个线程用于接收客户端的连接,也就是把这几句:
     while (true) {
                    Socket s = ss.accept();
                    new ServerThread(s).start();
                }

    换成:
    new Thread(new Runnable()
    {
    public void run()
    {
    while (true) {
                    Socket s = ss.accept();
                    new ServerThread(s).start();}
    }).start();
    没编译环境,没实验过,不过思想就是这样,具体语法有问题的话,自己修改一下。
      

  10.   


    曾经也遇到过这类问题,原因就是Socket的IO操作和Swing界面发生阻塞,使用多线程可以解决就是把Socket的IO操作另起一个线程,不要和Swing在同一个线程中,这样界面就不会卡死  while (true) {
                    Socket s = ss.accept();
                    new ServerThread(s).start();
                }改为用多线程实现就应该oK了试试吧