日期:2014-05-20 浏览次数:21222 次
。。。
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);
}
}
。。。略