我的聊天室程序。我有 2 个包,1 个用于客户端,其他用于服务器。运行服务器程序,填写端口字段并单击按钮启动服务器,窗口程序卡住,我无法对其执行任何操作,但服务器仍然工作,客户端仍然连接和聊天。
private void btnOpenActionPerformed(java.awt.event.ActionEvent evt) {
int port = Integer.parseInt(txtPort.getText());
go(port);
}
private void go(int port){
try {
listUser = new Hashtable<String, ClientConnect>();
server = new ServerSocket(port);
txaStatus.append("Server is started\n");
txaStatus.append("IP Server : "+InetAddress.getLocalHost().getHostAddress()+"\n");
txaStatus.append("Port : " + port + "\n");
while(true){
client = server.accept();
new ClientConnect(this,client);//class ClientConnect for DataOutPut & DataInput Client <=> Server
}
} catch (IOException e) {
txaStatus.append("Server cannot start\n");
JOptionPane.showMessageDialog(this,"Port busy","warning",JOptionPane.WARNING_MESSAGE);
System.exit(0);
}
}