日期:2014-05-20  浏览次数:21010 次

socket程序出错自动关闭
编写了一个socket服务器-客户端程序,当服务器和客户端连接成功,想生成新线程进行通信时程序自动关闭,
也没有抛出异常信息。哪位朋友能指点迷津啊???
还有,像这种程序直接关闭没有抛出异常信息的情况下,怎么debug啊?感觉无从下手!

Java code

。。。
SrvThread[] arrThreads = new SrvThread[50];
private int TotalThread = 0;
。。。略

//主程序 连接按钮
btnConnector.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent ae) {

    thread = new Thread() {
       public void run() {
          try {
        while (true) {
            if (svSocket == null) {
            svSocket = new ServerSocket(PORT_NO);
            }
                                                                mySocket = svSocket.accept();
                      String strLogMessage = mySocket.getInetAddress().getHostName();
            txtRecieve.setText(strLogMessage);
            arrThreads[TotalThread] = new SrvThread(TotalThread, mySocket);//这一步OK
            arrThreads[TotalThread].start();// [color=#FF0000]<- 这一步造成程序自动关闭!!![/color]                      TotalThread++;
                      if (stopServer == true && TotalThread >= 50) {
            break;
            }
        }

         } catch (SocketException se) {
        txtRecieve.setText(se.toString());
        System.exit(-1);
         } catch (Exception e) {
        txtRecieve.setText(e.toString());
        System.exit(-1);
         }
    }
     };
     thread.start();
  }
});

//通信线程
class SrvThread extends Thread {

    private int threadNo;
    User user = null;
    User matchedUser = null;
    private String errorMes = null;
    private boolean stopServer;

    public SrvThread(int threadNo, Socket mySocket) {

        this.threadNo = threadNo;
        this.stopServer = false;

        User user = new User(threadNo, mySocket);
        }

    public void run() {
        try {
        while (true) {
            user.ReceiveMessage();
                          if (stopServer == false && user.getReceiveMes() != null) {
              if (user.getMatchedUser() != null) {
                   user.getMatchedUser().SendMesssage(user.getReceiveMes());
              }
                }
                if (stopServer == true) {
               break;
                }
        }

        } catch (Exception e) {
            errorMes = e.toString();
            System.exit(-1);
        }
    }
。。。略




------解决方案--------------------
用debug模式不就好了?!